您如何从 then() 函数中的 Promise 访问内容,并在下一个 then() 函数中访问它
How do you access content from a Promise that is in a then() function, accessing it in the next then() function
如何从一个 then() 函数中的 Promise 访问内容,并在下一个 then() 函数中访问它。
我的问题大概是通过下面的代码来解释的
someRandomPromiseFunction().then(function(theArray) {
var newProm = _.map(theArray, function(arrayItem) {
return new Promise(function(resolve, reject) {
resolve(arrayItem);
});
}
Promise.all(newProm).then(function(theArray) {
return theArray; // How do I access this in the next then() function
}).catch(function(err) {
return err;
});
}).then(function(theArray) {
console.log(theArray); // I need to access theArray from the Promise.all HERE
});
只是return承诺
return Promise.all(newProm)...
当您 return 在 then()
回调中承诺时,外部承诺会等待内部承诺 resolves/rejects。如果内部承诺解决,则该值将传递到外部承诺中的下一个 then()
回调。否则,拒绝值将传递给外部承诺中的下一个失败回调。
new Promise((resolve,reject)=>{
resolve("Main promise");
}).then(function(){
return new Promise((resolve,reject)=>{
resolve();
}).then(()=>"Whosebug");
}).then(function(data){
console.log("Outer 'then()' data:",data);
});
如何从一个 then() 函数中的 Promise 访问内容,并在下一个 then() 函数中访问它。
我的问题大概是通过下面的代码来解释的
someRandomPromiseFunction().then(function(theArray) {
var newProm = _.map(theArray, function(arrayItem) {
return new Promise(function(resolve, reject) {
resolve(arrayItem);
});
}
Promise.all(newProm).then(function(theArray) {
return theArray; // How do I access this in the next then() function
}).catch(function(err) {
return err;
});
}).then(function(theArray) {
console.log(theArray); // I need to access theArray from the Promise.all HERE
});
只是return承诺
return Promise.all(newProm)...
当您 return 在 then()
回调中承诺时,外部承诺会等待内部承诺 resolves/rejects。如果内部承诺解决,则该值将传递到外部承诺中的下一个 then()
回调。否则,拒绝值将传递给外部承诺中的下一个失败回调。
new Promise((resolve,reject)=>{
resolve("Main promise");
}).then(function(){
return new Promise((resolve,reject)=>{
resolve();
}).then(()=>"Whosebug");
}).then(function(data){
console.log("Outer 'then()' data:",data);
});