I'm wondering if the following is a normal behavior?
Code
var when = require('when'); // I'm using when@3.7.4 node_modules/when
console.log("INIT");
when.promise(function(resolve, reject) {
return when.reject("false")
.then(function(ok) {
console.log("Step 1 - in then, ok = %s", ok);
return 'ok1';
}, function(err) {
console.log("Step 1.1 - in catch, err = %s", err);
return reject(err);
}).then(function(ok) {
console.log("Step 2 - in then, ok2 = %s", ok);
return resolve("done");
}).catch(function(err) {
console.log("Step 3 - in catch, err = %s", err);
return reject(err);
});
}).then(function(mainok) {
console.log("Step 9 - in main then, mainok = %s", mainok);
}).catch(function(err) {
console.log("Step 9 - in main catch, err = %s", err);
});
Here is the output I received when running it
INIT
Step 1.1 - in catch, err = false
Step 2 - in then, ok2 = undefined
Step 9 - in main catch, err = false
Reading the API I was expecting that step 1.1 would be called, then step 9 but not step 2.
Is that a bug or did I misread the API?
Thanks for your hints!
Yes, this is the expected behavior. Here are the steps I see:
resolve()
or reject()
to be called on it..then()
handler (e.g. the reject handler) and you see "Step 1.1" output..then()
handler for the rejection of the inner promise and you didn't throw or return a rejected promise from that handler, the state of the inner promise switches to resolved. If you "handle" a rejected promise and don't throw or return a rejected promise, then the state switches to fulfilled..then()
handler that is called is "Step 2" because the promise is now fulfilled, not rejected.resolve("done")
, but the outer promise has already been rejected to trying to resolve()
it now does nothing. It's state has already been set and cannot be changed..catch()
handler..then()
fulfilled handler and it goes to the last .catch()
handler "Step 9 - in main catch".Keep in mind that all promises are resolved or rejected asynchronously. Calling reject()
on the outer promise does not immediately run the .catch()
handler for the outer promise. It schedules it to run in the future.