Open In App

Reject Vs Throw Promises in JavaScript

This article covers the use of reject and throw premises in Javascript and explains it’s differences.
reject(): It is an inbuilt function in Javascript that returns a Promise object which has been rejected for a particular given reason.
 

Syntax:  



Promise.reject(reason)

Examples: The reason can be a simple string message or you can even pass an Error object.  




<script>
const p = new Promise( ( resolve, reject ) => {
 
   reject( 'promise failed!' );
 
});
p.catch(err => {
 
    console.log( err );
 
});
</script>

promise failed!




<script>
const p = new Promise( ( resolve, reject ) => {
 
   reject( new Error( 'promise failed!' ) );
 
});
p.catch( err => {
 
    console.log( err );
 
});
</script>

Error: promise failed!
    at :4:9
    at new Promise ()
    at :2:11
    at render (tryit.php:202)
    at tryit.php:170
    at dispatch (jquery.js:4435)
    at r.handle (jquery.js:4121)

throw: It is used in JavaScript to create and throw user defined exceptions. Using JavaScript throw statement, you can completely control program flow and generate the user define error messages. If we use throw instead of reject() in the above two examples the results will be exactly same (you can try it yourself just by replacing reject with throw).
Examples: However throw can be used in any Javascript try-catch block and not only with promises. 






<script>
const p = new Promise( ( resolve, reject ) => {
 
   throw( 'promise failed!' );
 
});
p.catch(err => {
 
    console.log( err );
 
});
</script>

promise failed!




<script>
var a = 20;
try
{
  if( a < 25 )
 
    throw ( 'Less than 25' );
 
  console.log( 'Okay!' );
}
catch(err)
{
  console.log( err );
}
</script>

Less than 25

Comparison between Promise- reject and throw:
1. If there is an asynchronous callback function inside the Promise then we cannot use throw from inside the callback function as it will not be recognised by catch() and we will be getting an error in the output.  




<script>
const p = new Promise( ( resolve, reject ) => {
     
    // Asynchronous function called within the Promise.
    setTimeout( () => { 
      throw( 'promise failed!' );
       
    }, 1000);
  });
 
  // The catch block will not be able to recognize the
  // error thrown. It will become an uncaught exception.
  p.catch( ( err )=> {
 
    console.log( err );
  });
</script>

/home/akarshan/Desktop/Projects/Personal/gfg/app.js:3
      throw( 'promise failed!' );
      ^
promise failed!
(Use `node --trace-uncaught ...` to show where the exception was thrown)




<script>
const p = new Promise( ( resolve, reject ) => {
 
    // Asynchronous function called within the Promise.
    setTimeout( () => {
 
      reject( 'promise failed!' );
       
    }, 1000);
  });
 
  // The catch block will be able to recognize
  // the rejected statement.
  p.catch( (err) => {
 
    console.log( err );
  });
</script>

promise failed!

2. This is a very basic difference. If throw is encountered anywhere inside a function the exception is thrown immediately and the control flow is terminated.In other words after throwing the exception control comes out of the function inside which the exception was thrown.  




<script>
const p = new Promise( ( resolve, reject ) => {
 
 
      throw( 'promise failed!' );     
 
      console.log("Here");
  });
 
p.catch( err => {
    console.log( err )
});
</script>

'promise failed!'




<script>
const p = new Promise( ( resolve, reject ) => {
 
      reject( 'promise failed!' );     
 
      console.log( "Here" );
  });
 
p.catch( err => {
 
    console.log( err )
});
</script>

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. If you use Promise.reject() in a try-catch block which is not associated with a promise, UnhandledPromiseRejectionWarning error will pop up.  




<script>
var a=20;
 
try{
 
if( a < 25 )
   
   Promise.reject ( 'Less than 25' );
 
console.log( 'Okay!' );
}
catch(err)
{
  console.log( "inside catch" );
 
  console.log( err );
}
</script>

Okay!




<script>
var a=20;
 
try{
 
if( a < 25 )
 
  throw ( 'Less than 25' );
 
console.log( 'Okay!' );
 
}
catch(err)
{
  console.log( "inside catch" );
 
  console.log( err );
}
</script>

inside catch
Less than 25

Article Tags :