【问题标题】:Return 2 variables using bluebird promises使用 bluebird promise 返回 2 个变量
【发布时间】:2015-07-23 14:48:43
【问题描述】:

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

【问题讨论】:

  • 你坚持哪一部分?你的问题更像是一个陈述。哪个部分不工作?
  • @atmd 我不知道如何返回 startthisfunction 的值。我想要另一个Helperfunction 立即返回的值
  • 否决这个,因为它只是糟糕的代码。只需调用一个函数,然后将结果作为参数传递给另一个函数。
  • @cleong 我不明白你为什么要否决这个。这是我遵循的方法,我不熟悉使用 Promise。这可能是完全错误的,但至少我尝试过。

标签: javascript node.js promise bluebird


【解决方案1】:

就像常规函数只有一个返回值一样,promise 也只有一个返回值,因为它是同一个类比。

就像使用常规函数一样,您可以从 Promise 返回复合值,也可以使用 .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
});

【讨论】:

  • Promise.resolve(1),会立即返回调用函数。你有延迟的理由吗?很抱歉我没能听懂你的回答
  • @user1692342 我只是放了两个承诺,延迟是为了说明它也适用于异步解决的承诺。
  • 我正在阅读有关传播功能的信息。据我了解,它将等待这两个承诺都结束,然后再使用传播功能。我想要做的是,立即返回第一个值。我的第二个承诺需要 2-3 分钟才能完成,一旦完成,我可以退回它
  • @user1692342 不,promise 不能这样做,它们是奇异值。如果你想要一个多值的东西,你需要一个异步迭代器(拉流)或一个可观察的(推流)。 Promise 代表了一个单一的价值,你不能通过它们的链“流动”。如果您愿意,我可以使用 RxJS 为 observables(我使用)添加答案。您是否需要等待两者都完成?有什么理由不能在 then 处理程序中只使用 .then 两个承诺吗?
  • 我等不及要完成这两个承诺的原因是我想在对请求进行基本预处理后立即向用户发送回复。一旦主要耗时的功能完成后,用户可以通过轮询来查看是否完成。
【解决方案2】:

似乎唯一错误的是期望do something with data; send 200 Ok to user; 应该在mainfunction() 中执行,部分通过callHelperfunction() 中的承诺链。

这可以通过多种方式克服。这是一对:

1.将do something with data; send 200 Ok to user; 移动到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!
        });
    });
}

2。完全放弃callHelperfunction(),一切尽在mainfunction()

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

【讨论】:

    猜你喜欢
    • 2021-01-11
    • 2015-03-04
    • 2015-02-18
    • 1970-01-01
    • 2014-05-10
    • 1970-01-01
    • 2022-07-08
    • 2017-12-15
    • 2018-01-04
    相关资源
    最近更新 更多