【发布时间】:2015-07-23 14:48:43
【问题描述】:
我正在尝试使用 Bluebird 库为 nodejs 编写一个 promise 函数。我想从我的函数中返回 2 个变量。 我希望第一个函数立即返回,第二个函数在返回之前完成自己的承诺链。
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!
})
});
}
【问题讨论】:
-
你坚持哪一部分?你的问题更像是一个陈述。哪个部分不工作?
-
@atmd 我不知道如何返回 startthisfunction 的值。我想要另一个Helperfunction 立即返回的值
-
否决这个,因为它只是糟糕的代码。只需调用一个函数,然后将结果作为参数传递给另一个函数。
-
@cleong 我不明白你为什么要否决这个。这是我遵循的方法,我不熟悉使用 Promise。这可能是完全错误的,但至少我尝试过。
标签: javascript node.js promise bluebird