承诺拒绝构造函数与抛出错误

在下面的代码中:

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 }); 

使用Promise api中的reject (在p2 )和抛出一个使用throw的错误(在p1 )是否有区别?

它完全一样?

如果它一样,为什么我们需要rejectcallback呢?

使用Promise api中的reject (在p2 )和抛出一个使用throw的错误(在p1 )是否有区别?

是的,你不能asynchronous使用throw ,而reject是一个callback。 例如,一些超时:

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

它完全一样?

不,至less在其他代码跟在你的陈述之后。 立即throw完成parsing器function,而调用reject继续执行正常 – 在“标记”承诺被拒绝后。

另外,如果throw错误对象,引擎可能会提供不同的exceptiondebugging信息。

对于你的具体例子,你是正确的, p1p2是从外界无法区分的。

不,没有,两者完全相同。 唯一的区别和为什么我们需要reject是当你需要asynchronous拒绝 – 例如,如果你正在转换基于callback的API,它可能需要发出asynchronous错误的信号。

 var p = new Promise(function(resolve, reject){ someCallbackApi(function(err, data){ if(err) reject(err); // CAN'T THROW HERE, non promise context, async. else resolve(data); }); }); 

我知道这有点晚了,但我并不认为这些答案都能完全回答我发现这个问题时的问题,下面是一个更完整的例子。

 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 }); 

一个非常有趣的观察是,如果使用throw ,将首先处理reject处理程序,然后处理error处理程序(如果拒绝处理程序不在位)。

用拒绝处理程序块

 var allowed = false; var p1 = new Promise( function(resolve, reject) { if (allowed) resolve('Success'); else // reject('Not allowed'); throw new Error('I threw an error') }) p1.then(function(fulfilled) { console.log('Inside resolve handler, resolved value: ' + fulfilled); }, function(rejected) { console.log('Inside reject handler, rejected value: ' + rejected); }).catch(function(error) { console.log('Inside error handler, error value: ' + error); })