Blair Davidson Blair Davidson - 9 months ago 79
Javascript Question

ES6 Promise Error Handling

My question is how errors are propagated through a series of then continuations to a catch continuation.


Promise.reject(new Error("some error"))
.then(v => v + 5)
.then(v => v + 15)
.catch(error => console.log(error));

After line 1 we have a rejected promise. When the continuation is invoked, .then(v => v + 5), is the original rejected promise returned as is. And the same applies for line 3. So no then continuations are run and once the catch continuation is reached we still have the same original object with the error as the result which is then passed to the catch continuation.

Is this assumption correct?

Answer Source

Yes, as is clear from the spec:

2.2.7. then must return a promise.

promise2 = promise1.then(onFulfilled, onRejected); If onRejected is not a function and promise1 is rejected, promise2 must be rejected with the same reason as promise1.

In other words, it is not exactly that the intermediate then's are "skipped", or that the promise "falls through" them (although one could sort of think of it that way); rather, the promises they return are rejected with the same reason as the input promise.

By the way, it is more accurate to call these "rejections", rather than "errors" (although, the "reason" for a rejection" would often be an error). Speaking of terminology, it would be more common to call these "handlers", not "continuations".