There's one difference — which shouldn't matter — that the other answers haven't touched on, so:
If the fulfillment handler passed to then
throws, the promise returned by that call to then
is rejected with what was thrown.
If it returns a rejected promise, the promise returned by the call to then
is resolved to that promise (and will ultimately be rejected, since the promise it's resolved to is rejected), which may introduce one extra async "tick" (one more loop in the microtask queue, to put it in browser terms).
Any code that relies on that difference is fundamentally broken, though. :-) It shouldn't be that sensitive to the timing of the promise settlement.
Here's an example:
function usingThrow(val) {
return Promise.resolve(val)
.then(v => {
if (v !== 42) {
throw new Error(`${v} is not 42!`);
}
return v;
});
}
function usingReject(val) {
return Promise.resolve(val)
.then(v => {
if (v !== 42) {
return Promise.reject(new Error(`${v} is not 42!`));
}
return v;
});
}
// The rejection handler on this chain may be called **after** the
// rejection handler on the following chain
usingReject(1)
.then(v => console.log(v))
.catch(e => console.error("Error from usingReject:", e.message));
// The rejection handler on this chain may be called **before** the
// rejection handler on the preceding chain
usingThrow(2)
.then(v => console.log(v))
.catch(e => console.error("Error from usingThrow:", e.message));
If you run that, as of this writing you get:
Error from usingThrow: 2 is not 42!
Error from usingReject: 1 is not 42!
Note the order.
Compare that to the same chains but both using usingThrow
:
function usingThrow(val) {
return Promise.resolve(val)
.then(v => {
if (v !== 42) {
throw new Error(`${v} is not 42!`);
}
return v;
});
}
usingThrow(1)
.then(v => console.log(v))
.catch(e => console.error("Error from usingThrow:", e.message));
usingThrow(2)
.then(v => console.log(v))
.catch(e => console.error("Error from usingThrow:", e.message));
which shows that the rejection handlers ran in the other order:
Error from usingThrow: 1 is not 42!
Error from usingThrow: 2 is not 42!
I said "may" above because there's been some work in other areas that removed this unnecessary extra tick in other similar situations if all of the promises involved are native promises (not just thenables). (Specifically: In an async
function, return await x
originally introduced an extra async tick vs. return x
while being otherwise identical; ES2020 changed it so that if x
is a native promise, the extra tick is removed where there is no other difference.)
Again, any code that's that sensitive to the timing of the settlement of a promise is already broken. So really it doesn't/shouldn't matter.
In practical terms, as other answers have mentioned:
- As Kevin B pointed out,
throw
won't work if you're in a callback to some other function you've used within your fulfillment handler — this is the biggie
- As lukyer pointed out,
throw
abruptly terminates the function, which can be useful (but you're using return
in your example, which does the same thing)
- As Vencator pointed out, you can't use
throw
in a conditional expression (? :
), at least not for now
Other than that, it's mostly a matter of style/preference, so as with most of those, agree with your team what you'll do (or that you don't care either way), and be consistent.
.then()
handler catches the thrown exception and turns it into a rejected promise automatically. Since I've read that thrown exceptions are not particularly fast to execute, I would guess that returning the rejected promise might be slightly faster to execute, but you'd have to devise a test in multiple modern browsers if that was important to know. I personally usethrow
because I like the readability. – jfriend00throw
is that it wouldn't result in a rejected promise if it was thrown from within an asynchronous callback, such as a setTimeout. jsfiddle.net/m07van33 @Blondie your answer was correct. – Kevin Breject
it from my param list. – Kevin B