那么如何处理promise中的if-else呢? [英] How to handle the if-else in promise then?

查看:18
本文介绍了那么如何处理promise中的if-else呢?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在某些情况下,当我从 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();
    }
})

但是我有两个问题:

  1. 我不确定在承诺中开始一个新的承诺然后流程是否是个好主意;

  1. 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?

推荐答案

只要你的函数返回一个 promise,你就可以使用你建议的第一种方法.

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
});

这篇关于那么如何处理promise中的if-else呢?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆