I was trying to follow along with the MDN promise.all example but it seems I cannot pass more arguments to the setTimeout callback.
var p1 = new Promise((resolve, reject) => {
setTimeout(resolve, 200, 1,2,3);
});
var p2 = new Promise((resolve, reject) => {
setTimeout(resolve, 500, "two");
});
Promise.all([p1, p2]).then(value => {
console.log(value);
}, reason => {
console.log(reason)
});
This prints [1, "two"]
, where I would expect [1, 2, 3, "two"]
. Doing this with setTimeout
without promise fulfillment works as expected
setTimeout(cb, 100, 1, 2, 3);
function cb(a, b, c){
console.log(a, b, c);
}
//=>1 2 3
Why doesn't this work with the promise? How can it be made to work with the promise?
The resolve
function only takes a single argument. You can't change that as this is the spec.
Passing more than that doesn't make a difference, and your other values are ignored.
You can opt to pass an array to the resolve
function, but you'll get an array on the then
handler, not independent values like you wanted.
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