Javascript 使用bluebird承诺返回2个变量

Javascript 使用bluebird承诺返回2个变量,javascript,node.js,promise,bluebird,Javascript,Node.js,Promise,Bluebird,我正在尝试使用Bluebird库为nodejs编写一个promise函数。我想从函数中返回2个变量。 我希望第一个函数立即返回,第二个函数在返回之前完成自己的承诺链 function mainfunction() { return callHelperfunction() .then(function (data) { //do something with data //send 200 Ok to user }) .then(f

我正在尝试使用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!
        })
    });
}

就像正则函数只有一个返回值一样,同样地,由于这是相同的类比,所以只能解析一个值

与常规函数一样,您可以从承诺中返回复合值,也可以使用
.spread
轻松使用它,如果您返回数组:

Promise.resolve().then(function(el){
     return [Promise.resolve(1), Promise.delay(1000).return(2));
}).spread(function(val1, val2){
      // two values can be accessed here
      console.log(val1, val2); // 1, 2
});

唯一似乎是错误的是期望
处理数据;向用户发送200 Ok应在
mainfunction()
中执行,部分通过
callHelperfunction()中的承诺链执行

这可以通过多种方式克服。这里有几个:

1。移动
处理数据;向用户发送200 Ok
进入
callHelperfunction()

function mainfunction() {
    return callHelperfunction())
    .catch(function (err) {
        //handle errors
    });
}

function callHelperfunction() {
    return anotherHelperFunction()
    .then(function (data1) {
        //do something with data
        //send 200 Ok to user
        return startthisfunction(data1)
        .then(function (data2) {
            //wait for response from startthisfunction here
            //do something more!
        });
    });
}
function mainfunction() {
    return anotherHelperFunction()
    .then(function (data1) {
        //do something with data1
        //send 200 Ok to user
        return startthisfunction(data1);
    })
    .then(function (data2) {
        //wait for response from startthisfunction here
    })
    .catch(function (err) {
        //handle errors
    });
}
2。完全不用调用调用HelperFunction()
,在
mainfunction()中执行所有操作。

function mainfunction() {
    return callHelperfunction())
    .catch(function (err) {
        //handle errors
    });
}

function callHelperfunction() {
    return anotherHelperFunction()
    .then(function (data1) {
        //do something with data
        //send 200 Ok to user
        return startthisfunction(data1)
        .then(function (data2) {
            //wait for response from startthisfunction here
            //do something more!
        });
    });
}
function mainfunction() {
    return anotherHelperFunction()
    .then(function (data1) {
        //do something with data1
        //send 200 Ok to user
        return startthisfunction(data1);
    })
    .then(function (data2) {
        //wait for response from startthisfunction here
    })
    .catch(function (err) {
        //handle errors
    });
}

你被卡在哪一部分?你的问题更像是一种陈述。哪个部分不起作用?@atmd我无法获取如何返回start此函数的值。我希望另一个helper函数立即返回此值,因为它只是一个坏代码。只需调用一个函数,然后将结果作为参数传递给另一个函数。@cleong我不明白你为什么要否决这个。这是我遵循的方法,我对使用承诺是新手。这可能是完全错误的,但至少我尝试过。Promise.resolve(1)将立即返回调用函数。你有什么理由拖延吗?对不起,我听不懂你的话answer@user1692342我只是在这里写了两个承诺,延迟是为了说明它也可以与异步解析的承诺一起工作。据我所知,它将等待这两个承诺,以获得过去之前,到蔓延功能。我要做的是,立即返回第一个值。我的第二个承诺需要2-3分钟才能完成,一旦完成,我就可以回来了it@user1692342不,承诺不能做到这一点,它们是奇异值。如果您想要多值的东西,您需要异步迭代器(拉流)或可观察的(推流)。承诺代表一个你不能“流动”过他们的链的奇异值。如果您愿意,我可以使用RxJS为可观察对象添加一个答案(我使用)。你是否需要等待两者都完成?有什么原因不能只执行
。然后执行
然后执行
处理程序中的两个承诺吗?我不能等待这两个承诺都完成的原因是我想在对请求进行基本预处理后立即向用户发送回复。一旦完成了主要耗时的功能,用户就可以进行轮询以确定它是否完成。