0
new Promise(async (resolve, reject) =>
{
  try
  {
    await new Promise((resolveSec, rejectSec) =>
    {
      setTimeout(function()
      {
        resolveSec('')
      }, 1000);
    }).then(function(val)
    {
      return reject('should reject') //does not stop execution
      //throw('should reject'); //stops execution
    });
    console.log('not here'); //should not get to here
  }
  catch (err)
  {
    console.log('caught error');
  }
}).catch(function(err)
{
  console.log(err);
});

Could someone explain why the above code does not end execution on "reject" but it works with "throw" as I thought that both "reject" and "throw" inside Promise have same functionality.

dfilkovi
  • 3,051
  • 7
  • 39
  • 51

1 Answers1

0

Cause throw ends the inner Promises execution, which will then be rethrown by the await statement, ending the execution of the outer Promise. reject however will set the outer promises state to rejected, return will end the execution of the inner promisee function but not of the outer one.

Jonas Wilms
  • 132,000
  • 20
  • 149
  • 151