I need to pass a variable through a promise .then
chain. I can't find a way to work it out. I'm quite new to this so bear with me!
return foo.bar(baz)
.then((firstResult) => {
let message = firstResult;
})
.then(() => foo.bar(qux)
.then((secondResult) => {
message =+ secondResult;
console.log(message);
})
)
What's the correct way of doing this please?
Don't use a promise chain for this.
You are making two, independent calls to foo.bar()
and neither of them depend on the results of the other.
Make them independently, and get all their data at the end with Promise.all.
var promises = [
foo.bar(baz),
foo.bar(qux)
];
Promise.all(promises).then( results => {
let message = results[0] + results[1];
console.log(message);
});
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With