I'm using bluebird
as my Promise
libaray. I have the following chain:
function a() {
return Promise.all([promise1, promise2]).then((results) => {
return promise3(results)
})
}
But I couldn't get the result of promise3
, I mean the following doesn't work:
a().then((result) => {
console.log(result)
}).catch((err) => {
console.error(err)
})
I searched and read about promises but I couldn't understand where the problem is?
This could be for several reasons and it is impossible to determine which from the post. So to help future readers - let's figure out what can go wrong in the above scenario:
Let's make the following adjustments to the code:
function a() {
console.log("a called"); // if this doesn't log, you're redefining 'a'
return Promise.all([promise1, promise2]).then((results) => {
console.log("In .all"); // if this isn't called, promise1 or promise2 never resolve
return promise3(results);
}).tap(() =>
console.log("Promise 3 fn resolved") // if this doesn't log, promise3 never resolves
});
}
Then, our then
handler:
let r = a().then((result) => {
console.log('result', result); // this logs together with promise 3 fn
}); // bluebird automatically adds a `.catch` handler and emits unhandledRejection
setTimeout(() => {
// see what `r` is after 30 seconds, if it's pending the operation is stuck
console.log("r is", r.inspect());
}, 30000);
That should give you full coverage for all cases that could possible go wrong.
If I had to guess - one of the promises never resolves since you have a Promise
constructor somewhere that is never calling resolve
or reject
.
(Bluebird will warn you against this in certain scenarios - make sure you have warnings on)
Another scenario which could cause this is cancellation - but I'm assuming you don't have that turned on.
Happy coding.