Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Promise constructor with reject call vs throwing error

In the following code:

var p1 = new Promise(function (resolve, reject) {     throw 'test1'; });  var p2 = new Promise(function (resolve, reject) {     reject('test2'); });  p1.catch(function (err) {     console.log(err); // test1 });  p2.catch(function (err) {     console.log(err); // test2 }); 

Is there any difference between using reject (in p2) from the Promise api, and throwing an error (in p1) using throw?

Its exactly the same?

If its the same, why we need a reject callback then?

like image 518
lante Avatar asked Feb 24 '15 18:02

lante


People also ask

Is Promise reject the same as throwing error?

Yes, the biggest difference is that reject is a callback function that gets carried out after the promise is rejected, whereas throw cannot be used asynchronously.

What happens when you throw an error in Promise?

In case of an error, the promise becomes rejected, and the execution should jump to the closest rejection handler. But there is none. So the error gets “stuck”. There's no code to handle it.

What are the two parameters passed into Promise constructor?

It receives two functions as parameters: resolutionFunc and rejectionFunc . Any errors thrown in the executor will cause the promise to be rejected, and the return value will be neglected.

What happens if multiple resolve or reject is called within a Promise?

The only thing to understand is that once resolved (or rejected), that is it for a defered object - it is done. If you call then(...) on its promise again, you immediately get the (first) resolved/rejected result. Additional calls to resolve() will not have any effect.


2 Answers

Is there any difference between using reject (in p2) from the Promise api, and throwing an error (in p1) using throw?

Yes, you cannot use throw asynchronously, while reject is a callback. For example, some timeout:

new Promise(_, reject) {     setTimeout(reject, 1000); }); 

Its exactly the same?

No, at least not when other code follows your statement. throw immediately completes the resolver function, while calling reject continues execution normally - after having "marked" the promise as rejected.

Also, engines might provide different exception debugging information if you throw error objects.

For your specific example, you are right that p1 and p2 are indistinguishable from the outside.

like image 81
Bergi Avatar answered Sep 24 '22 09:09

Bergi


I know this is a bit late, but I don't really think either of these answers completely answers the questions I had when I found this, Here is a fuller example to play with.

var p1 = new Promise(function (resolve, reject) {      throw 'test 1.1'; //This actually happens      console.log('test 1.1.1'); //This never happens      reject('test 1.2'); //This never happens because throwing an error already rejected the promise      console.log('test 1.3'); //This never happens  });    var p2 = new Promise(function (resolve, reject) {      reject('test 2.1'); //This actually happens      console.log('test 2.1.1'); //This happens BEFORE the Promise is rejected because reject() is a callback      throw 'test 2.2'; //This error is caught and ignored by the Promise      console.log('test 2.3'); //This never happens  });    var p3 = new Promise(function (resolve, reject) {      setTimeout(function() { reject('test 3.1');}, 1000); //This never happens because throwing an error already rejected the promise      throw('test 3.2'); //This actually happens      console.log('test 3.3'); //This never happens  });    var p4 = new Promise(function (resolve, reject) {      throw('test 4.1'); //This actually happens      setTimeout(function() { reject('test 4.2');}, 1000); //This never happens because throwing an error already rejected the promise      console.log('test 4.3'); //This never happens  });    var p5 = new Promise(function (resolve, reject) {      setTimeout(function() { throw('test 5.1');}, 1000); //This throws an Uncaught Error Exception      reject('test 5.2'); //This actually happens      console.log('test 5.3'); //This happens BEFORE the Promise is rejected because reject() is a callback  });    var p6 = new Promise(function (resolve, reject) {      reject('test 6.1'); //This actually happens      setTimeout(function() { throw('test 6.2');}, 1000); //This throws an Uncaught Error Exception      console.log('test 6.3'); //This happens BEFORE the Promise is rejected because reject() is a callback  });      p1.then(function (resolve) {      console.log(resolve, "resolved")  }, function (reject) {      console.log(reject, "rejected")  }).catch(function (err) {      console.log(err, "caught"); // test1  });    p2.then(function (resolve) {      console.log(resolve, "resolved")  }, function (reject) {      console.log(reject, "rejected")  }).catch(function (err) {      console.log(err, "caught"); // test2  });    p3.then(function (resolve) {      console.log(resolve, "resolved")  }, function (reject) {      console.log(reject, "rejected")  }).catch(function (err) {      console.log(err, "caught"); // test3  });    p4.then(function (resolve) {      console.log(resolve, "resolved")  }, function (reject) {      console.log(reject, "rejected")  }).catch(function (err) {      console.log(err, "caught"); // test4  });    p5.then(function (resolve) {      console.log(resolve, "resolved")  }, function (reject) {      console.log(reject, "rejected")  }).catch(function (err) {      console.log(err, "caught"); // test5  });    p6.then(function (resolve) {      console.log(resolve, "resolved")  }, function (reject) {      console.log(reject, "rejected")  }).catch(function (err) {      console.log(err, "caught"); // test6  });
like image 23
Jonathan Rys Avatar answered Sep 20 '22 09:09

Jonathan Rys