Jam*_*ser 1 javascript resolve promise access
如何从then()函数中的Promise访问内容,在next 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
});
Run Code Online (Sandbox Code Playgroud)
回报承诺
return Promise.all(newProm)...
Run Code Online (Sandbox Code Playgroud)
当你在then()回调函数中返回一个promise时,外部的promise会等到内部的一个解析/拒绝.如果内部promise继续解析,那么该值将传递then()给外部promise中的下一个回调.否则,拒绝值将传递给外部promise中的下一个失败回调.
new Promise((resolve,reject)=>{
resolve("Main promise");
}).then(function(){
return new Promise((resolve,reject)=>{
resolve();
}).then(()=>"Stackoverflow");
}).then(function(data){
console.log("Outer 'then()' data:",data);
});Run Code Online (Sandbox Code Playgroud)