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
1 Answer
Reset to default 8You 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' }
).