【发布时间】:2016-01-20 08:06:49
【问题描述】:
在某些情况下,当我从 promise 对象中获取返回值时,我需要根据值的条件启动两个不同的 then() 进程,例如:
promise().then(function(value){
if(//true) {
// do something
} else {
// do something
}
})
我在想也许我可以这样写:
promise().then(function(value){
if(//true) {
// call a new function which will return a new promise object
ifTruePromise().then();
} else {
ifFalsePromise().then();
}
})
但是有了这个,我有两个问题:
我不确定在一个 Promise 中启动一个新的 Promise-then 流程是否是个好主意;
如果我需要两个进程最后调用一个函数怎么办?这意味着它们具有相同的“终端”
我试图返回新的承诺以保留原始链,例如:
promise().then(function(value){
if(//true) {
// call a new function which will return a new promise object
// and return it
return ifTruePromise();
} else {
// do something, no new promise
// hope to stop the then chain
}
}).then(// I can handle the result of ifTruePromise here now);
但在这种情况下,无论是真还是假,下一个then都会起作用。
那么,处理它的最佳做法是什么?
【问题讨论】:
-
这就是你要找的东西stackoverflow.com/questions/26599798/… ?
标签: node.js asynchronous promise