I have read several articles on this subject, but it is still not clear to me if there is a difference between Promise.reject
vs. throwing an error. For example,
Using Promise.reject
return asyncIsPermitted() .then(function(result) { if (result === true) { return true; } else { return Promise.reject(new PermissionDenied()); } });
Using throw
return asyncIsPermitted() .then(function(result) { if (result === true) { return true; } else { throw new PermissionDenied(); } });
My preference is to use throw
simply because it is shorter, but was wondering if there is any advantage of one over the other.
The static Promise. reject function returns a Promise that is rejected. For debugging purposes and selective error catching, it is useful to make reason an instanceof Error .
A Promise rejection indicates that something went wrong while executing a Promise or an async function. Rejections can occur in several situations: throwing inside an async function or a Promise executor/then/catch/finally callback, when calling the reject callback of an executor , or when calling Promise.
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.
We must always add a catch() , otherwise promises will silently fail. In this case, if thePromise is rejected, the execution jumps directly to the catch() method. You can add the catch() method in the middle of two then() methods, but you will not be able to break the chain when something bad happens.
Here promise failed! 3. The reject can only be used with a Javascript promise but throw unlike reject can be used to create and throw user-defined exceptions in any try-catch block and not only the ones with promises.
A Promise that is rejected with the given reason. The static Promise.reject function returns a Promise that is rejected. For debugging purposes and selective error catching, it is useful to make reason an instanceof Error. The definition of 'Promise.reject' in that specification.
Any time you are inside of a promise callback, you can use throw. However, if you’re in any other asynchronous callback, you must use reject. Instead you’re left with an unresolved promise and an uncaught exception. That is a case where you would want to instead use reject. However, you could fix this in two ways.
Examples: However throw can be used in any Javascript try-catch block and not only with promises. Program 1: Using throw in a promise. throw( 'promise failed!' ); promise failed! Program 2: Using throw without a promise. console.log ( 'Okay!' );
There is no advantage of using one vs the other, but, there is a specific case where throw
won't work. However, those cases can be fixed.
Any time you are inside of a promise callback, you can use throw
. However, if you're in any other asynchronous callback, you must use reject
.
For example, this won't trigger the catch:
new Promise(function() { setTimeout(function() { throw 'or nah'; // return Promise.reject('or nah'); also won't work }, 1000); }).catch(function(e) { console.log(e); // doesn't happen });
Instead you're left with an unresolved promise and an uncaught exception. That is a case where you would want to instead use reject
. However, you could fix this in two ways.
new Promise(function(resolve, reject) { setTimeout(function() { reject('or nah'); }, 1000); }).catch(function(e) { console.log(e); // works! });
function timeout(duration) { // Thanks joews return new Promise(function(resolve) { setTimeout(resolve, duration); }); } timeout(1000).then(function() { throw 'worky!'; // return Promise.reject('worky'); also works }).catch(function(e) { console.log(e); // 'worky!' });
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With