最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Send multiple arguments in .then function - Stack Overflow

programmeradmin2浏览0评论

In callbacks we can send as many arguments as we want.

Likewise, I want to pass multiple arguments to a then function, either in Bluebird promises or native JavaScript promises.

Like this:

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

In callbacks we can send as many arguments as we want.

Likewise, I want to pass multiple arguments to a then function, either in Bluebird promises or native JavaScript promises.

Like this:

myPromise.then(a => {
    var b=122;
    // here I want to return multiple arguments
}).then((a,b,c) => {
    // do something with arguments
});
Share Improve this question edited Apr 29, 2017 at 15:15 Mark Amery 156k90 gold badges430 silver badges472 bronze badges asked Apr 29, 2017 at 13:28 hardyhardy 9012 gold badges14 silver badges31 bronze badges 1
  • pass object with properties – Edgar Commented Apr 29, 2017 at 13:31
Add a ment  | 

1 Answer 1

Reset to default 8

You can simply return an object from the then method. If you use destructuring in the next then, it will be like passing multiple variables from one then to the next:

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

Note that in the first then, we are using a shortcut for returning a and b (it's the same as using { a: a, b: b, c: 'foo' }).

发布评论

评论列表(0)

  1. 暂无评论