The important thing to know is that the .then() method is always chained onto a Promise, and it returns a new Promise whose value and resolved/rejected state is based on what the function given to it returned.
In your example, if the original Promise resolves, then the first function in your first .then() will get called with the resolved value. If it returns a value then whatever value it returns will then get ultimately passed into the first function in your second .then(). The function in catch will never get called.
If the Promise rejects, the second function in your first .then() will get called with the rejected value, and whatever value it returns will become a new resolved Promise which passes into the first function of your second then. Catch is never called here either. It's only if the Promise rejects and you keep returning rejected Promises or throwing errors in both your function(err){}
functions that you'll get the function(err){}
in your catch block called.
To resolve in your function(data){}
functions, all you need to do is return a value (or return a Promise/thenable that later resolves).
To reject, you would need to either throw an error, actually cause an error, return a new Promise that eventually rejects, or explicitly return Promise.reject(::some value::)
.
To resolve in your function(err){}
blocks, all you need to do is return a new value. You could also return a Promise, in which case that Promise is what will be returned (eventually resolving or rejecting).
In general, it's not wise to define both the resolved and rejected path in the same .then() though: PROMISE.then(fn).catch(fn)
is a much safer/clearer practice, because then any errors in the first .then() will be caught by catch. If you do PROMISE.then(fn, fn)
instead though, if an error happens in the first function it would NOT get caught by the second: some later chained on method would have to catch it.
undefined
) but want the nextcatch
to get the error as well, for which they need to rethrow it. – Bergithen
, and the rejection will be passed on automatically. – Bergi