【问题标题】:Send multiple arguments in .then function在 .then 函数中发送多个参数
【发布时间】:2017-09-27 12:28:39
【问题描述】:

在回调中,我们可以发送任意数量的参数。

同样,我想将多个参数传递给 then 函数,无论是在 Bluebird 承诺还是原生 JavaScript 承诺中。

像这样:

myPromise.then(a => {
    var b=122;
    // here I want to return multiple arguments
}).then((a,b,c) => {
    // do something with arguments
});

【问题讨论】:

  • 通过属性传递对象

标签: javascript callback ecmascript-6 promise bluebird


【解决方案1】:

您可以简单地从then 方法返回一个对象。如果你在下一个then 中使用destructuring,这就像将多个变量从一个then 传递到下一个:

myPromise.then(a => {
    var b = 122;
    return {
        a,
        b,
        c: 'foo'
    };
}).then(({ a, b, c }) => {
    console.log(a);
    console.log(b);
    console.log(c);    
});

请注意,在第一个then 中,我们使用了返回ab 的快捷方式(与使用{ a: a, b: b, c: 'foo' } 相同)。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2022-01-05
    • 1970-01-01
    • 2010-12-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多