(Related but not quite the same: JS Promises: Fulfill vs Resolve)
I\'ve been trying to wrap my head around Javascript promises, and I\'m struggling with the basic no
Let's add emphasis somewhere else in one of your quotes :
A promise is resolved if trying to resolve or reject it has no effect, i.e. the promise has been "locked in" to either follow another promise, or has been fulfilled or rejected
reject()
will change your Promise and all the prior-chained ones status to rejected
.
While resolve()
will lock your current Promise, but only the fulfillment of the resolving callbacks will set its status to fulfilled
. As long as everything is not finished, your Promise will still be 'pending'
.
e.g If you do chain your Promise, and that during the chain, an error is thrown, then the state of your Promise is set to 'rejected'
.
var p = new Promise((resolve, reject) => {
resolve(); // here p is resolved, we can't call resolve() or reject() anymore
}).then((e) => { // chain it
return new Promise((resolve, reject) => {
console.log(p); // 'pending' because it's still chained
reject('whatever') // this one throws an error and breaks the chain
})
}).then(() => console.log('passed')); // won't happen
setTimeout(() => console.log(p, "please check in your browser's console"), 1000); // rejected
So even if you are resolve()
-ing the current operation of your Promise, you can't know what will happen after, so you can't know if at the end it will be fulfilled
or rejected
.