Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JavaScript Promises - reject vs. throw

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.

like image 965
Naresh Avatar asked Oct 30 '15 21:10

Naresh


People also ask

What happens when you reject a Promise JavaScript?

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 .

Why do Promises get rejected?

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.

What happens when Promise is rejected?

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.

How do you handle reject promises?

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.

What is the difference between reject and throw in JavaScript?

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.

What is the use of reject promise in JavaScript?

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.

Should I use throw or reject in a promise callback?

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.

What are some examples of throw in JavaScript?

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!' );


1 Answers

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.

  1. by using the original Promise's reject function inside the timeout:

new Promise(function(resolve, reject) {   setTimeout(function() {     reject('or nah');   }, 1000); }).catch(function(e) {   console.log(e); // works! });
  1. by promisifying the timeout:

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!' });
like image 113
Kevin B Avatar answered Oct 09 '22 16:10

Kevin B