use*_*342 2 javascript node.js promise bluebird
我正在尝试使用Bluebird库为nodejs编写一个promise函数.我想从我的函数中返回2个变量.我希望第一个函数立即返回,第二个函数在返回之前完成自己的promise链.
function mainfunction() {
return callHelperfunction()
.then(function (data) {
//do something with data
//send 200 Ok to user
})
.then(function (data2) {
//wait for response from startthisfunction here
})
.catch(function (err) {
//handle errors
});
}
function callHelperfunction() {
return anotherHelperFunction()
.then(function (data) {
return data;
return startthisfunction(data)
.then(function () {
//do something more!
})
});
}
Run Code Online (Sandbox Code Playgroud)
就像常规函数只有一个返回值一样,类似的promises只解析一个值,因为它是相同的类比.
就像常规函数一样,您可以从promise中返回一个复合值,.spread如果返回一个数组,也可以使用它来轻松使用它:
Promise.resolve().then(function(el){
return [Promise.resolve(1), Promise.delay(1000).return(2));
}).spread(function(val1, val2){
// two values can be accessed here
console.log(val1, val2); // 1, 2
});
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
773 次 |
| 最近记录: |