问题描述
在某些情况下,当我从promise对象获得返回值时,我需要根据值的条件来启动两个不同的then()
进项,例如:
In some case, when I get a return value from a promise object, I need to start two different then()
precesses depend on the value's condition, like:
promise().then(function(value){
if(//true) {
// do something
} else {
// do something
}
})
我在想也许我可以这样写:
I'm thinking maybe I can write it like:
promise().then(function(value){
if(//true) {
// call a new function which will return a new promise object
ifTruePromise().then();
} else {
ifFalsePromise().then();
}
})
但是,我有两个问题:
-
我不确定开始一个新的promise-然后在promise中进行处理是否是个好主意;
I'm not sure if it's a good idea to start a new promise-then process in a promise;
如果我需要两个进程来最后调用一个函数,该怎么办?这意味着它们具有相同的终端"
what if I need the two process to call one function in the last? It means they have the same "terminal"
我试图返回新的承诺,以保持原来的链条,如:
I tried to return the new promise to keep the original chain like:
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
都可以使用.
but in this case, whether it's true or false, the next then
will work.
那么,处理它的最佳实践是什么?
SO, what's the best practice to handle it?
推荐答案
只要您的函数返回承诺,就可以使用您建议的第一种方法.
As long as your functions return a promise, you can use the first method that you suggest.
下面的小提琴显示了如何根据第一个解析的值采用不同的链接路径.
The fiddle below shows how you can take different chaining paths depending on what the first resolved value will be.
function myPromiseFunction() {
//Change the resolved value to take a different path
return Promise.resolve(true);
}
function conditionalChaining(value) {
if (value) {
//do something
return doSomething().then(doSomethingMore).then(doEvenSomethingMore);
} else {
//do something else
return doSomeOtherThing().then(doSomethingMore).then(doEvenSomethingMore);
}
}
function doSomething() {
console.log("Inside doSomething function");
return Promise.resolve("This message comes from doSomeThing function");
}
function doSomeOtherThing() {
console.log("Inside doSomeOtherthing function");
return Promise.resolve("This message comes from doSomeOtherThing function");
}
function doSomethingMore(message) {
console.log(message);
return Promise.resolve("Leaving doSomethingMore");
}
function doEvenSomethingMore(message) {
console.log("Inside doEvenSomethingMore function");
return Promise.resolve();
}
myPromiseFunction().then(conditionalChaining).then(function () {
console.log("All done!");
}).
catch (function (e) {
});
您也可以只进行一个条件链接,将返回承诺分配给变量,然后继续执行应以任何一种方式运行的功能.
You can also just make one conditional chaining, assign the return promise to a variable and then keep executing the functions that should be run either way.
function conditionalChaining(value){
if (value) {
//do something
return doSomething();
} else{
//do something else
return doSomeOtherThing();
}
}
var promise = myPromiseFunction().then(conditionalChaining);
promise.then(function(value){
//keep executing functions that should be called either way
});
这篇关于那么如何处理if-else中的诺言?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!