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?
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.
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.
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.
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.
Is there any difference between using
reject
(inp2
) from thePromise
api, and throwing an error (inp1
) usingthrow
?
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.
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 });
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