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

但我有两个问题:

  1. 我不确定开始一个新的promise ,然后在promise 中进行处理是否是一个好主意;

  2. 如果我需要这两个进程在最后一个进程中调用一个函数呢?这意味着他们有相同的"终端"

我试图回报新的promise ,保持原来的链条,比如:

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个都会起作用.

那么,处理这个问题的最佳做法是什么?

推荐答案

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

下面的提琴显示了如何根据第一个解析值 Select 不同的链接路径.

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) {

});

您也可以只进行一个条件链接,将返回promise 分配给一个变量,然后继续执行应该以任何方式运行的函数.

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

Node.js相关问答推荐

node 无法验证第一个证书

可以删除一个mongodb catch块

express返回意外的URL

如何使用聚合管道交换键值对

一个函数中的两个依赖的NodeJS数据库操作.如果第二个失败了怎么办?

如何在医学中按patientId查找一个并同时插入多个数据

下一个API路由如何处理多个并发请求?

通过 Node js 中的 TBA 执行 netsuite REST upsert 操作出现 401 错误

Postgressql的BIGSERIAL自增序列,即使由于唯一约束错误没有创建行,也会自动增加

npm 在 Windows 终端中不工作

如何使用 NodeJS 加密模块将 ECDH 密钥转换为 PEM 格式

将 express js app.use() 移动到另一个文件

如何让 vscode 假设一个 node.js 上下文,以便它不假设 `fetch` 存在?

baseurl64 缓冲区解码

将 myproject/.npmrc 与注册表一起使用

ChildProcess 关闭、退出事件之间的区别

什么是 JavaScript 中的REPL?

请求新页面时如何将 AngularJS 路由与 Express (Node.js) 一起使用?

node.js 服务器和 HTTP/2 (2.0) 与 express.js

mongoose 填充与对象嵌套