Background
This question suggests that using throw
inside a promise function is effectively identical to calling the reject
callback.
e.g. These are equivalent:
new Promise(function(resolve, reject) {
throw new Error("sadface"); // Using throw
}).catch(function(e) {
// ... handle error
});
new Promise(function(resolve, reject) {
reject(new Error("sadface")); // Using reject
}).catch(function(e) {
// ... handle error
});
Question
Obviously, if there's async code involved (such as an database or HTTP request), you can't use throw
, since the stack has changed.
As a "best practice", should I then always use reject
inside a promise to keep things consistent? Or should throw
still be used under certain circumstances?