简体   繁体   English

Javascript承诺链接多个错误

[英]Javascript Promises Chaining Multiple Errors

How come that all my Resolved Callbacks are properly executed but only 1 Error Callback is being run ? 为什么我所有已解决的回调均正确执行,但仅运行1个错误回调?

In the example here if longOperation(true) I get only one error, while when run with false I get the two resolved messages. 在这里的示例中,如果longOperation(true)只会出现一个错误,而当运行false时longOperation(true)得到两个已解决的消息。 In my understanding each .then returns a new promise which is in the error state for the second .then error callback registration 以我的理解,每个.then返回一个新的promise,它在第二个.then错误回调注册中处于错误状态

This example is written using Typescript ! 这个例子是用Typescript编写的!

 function longOperation(error) { return new Promise < any > ((resolve, reject) => { if (error) { reject("Error") } else { setTimeout(function() { resolve(true) }, 1000) } }) } let op = longOperation(true); op .then(result => { console.log("Resolved #1") }, undefined) .then(result => { console.log("Resolved #2") }, undefined) .then(undefined, error => { console.log("Error1") }) .then(undefined, error => { console.log("Error2") }) 

The first error handler catches the error, just like a regular catch block. 第一个错误处理程序捕捉错误,就像常规的catch块一样。 If you want the chained promise to be rejected, you have to rethrow the error: 如果您希望链式承诺被拒绝,则必须重新抛出错误:

 Promise.reject().then(() => { console.log(1); }).then(() => { console.log(2); }).then(null, e => { console.log(3); throw e; // <= rethrow here }).then(null, e => { console.log(4); }); 

Promises can "recover". 承诺可以“恢复”。

The error will propagate down the chain of ".then"s until it finds an error callback. 错误将一直沿“ .then”链传播,直到找到错误回调为止。 When it does it will execute the error callback and pass the return value down to the next promise's resolve method. 这样做时,它将执行错误回调,并将返回值传递给下一个promise的resolve方法。

So only the next closest error callback to the promise will get executed. 因此,只有下一个最接近promise的错误回调才会被执行。

If you want to catch all possible errors along the chain just put your error callback at the end of the chain and all errors will be handled by it. 如果您想捕获链中所有可能的错误,只需将错误回调放在链的末尾,所有错误都将由该错误处理。

In your code example you are actually creating 5 promises. 在您的代码示例中,您实际上在创建5个Promise。 The original one and 1 for each ".then". 每个“ .then”的原始1和1。 The 2nd and 3rd only have an onResolved method and the last two only have and onRejected method. 第2个和第3个仅具有onResolved方法,后两个仅具有andonRejected方法。

This is how it will be executed: 这是将如何执行:

1st Promise(op): I just rejected. 第一承诺(op):我只是拒绝了。 I see I have a deferred promise with no onRejected callback. 我看到我有一个没有onRejected回调的延迟承诺。 So I'll just call it's reject method with the same value I rejected with. 因此,我将其称为具有相同值的拒绝方法。

2nd Promise: The previous promise called my reject method and I also have a deferred promise with no onRejected callback. 第二个承诺:先前的承诺称为我的拒绝方法,我也有一个没有onRejected回调的递延承诺。 So I'll just call it's reject method with the same value I rejected with. 因此,我将其称为具有相同值的拒绝方法。

3rd Promise: The previous promise called my reject method. 第三承诺:先前的承诺称为我的拒绝方法。 I have a deferred promise too, but mine does have an onRejected callback. 我也有一个延迟的承诺,但是我的确有一个onRejected回调。 I,ll call the callback with the value I rejected with, and I'll call my deferred promise's resolve method with the callback's return value. 我将使用拒绝的值来调用该回调,并使用该回调的返回值来调用我的延后Promise的resolve方法。

4th Promise: The previous promise called my resolve method. 第四个承诺:先前的承诺称为我的解决方法。 I have a deferred promise, but it doesn't have an onResolved callback so I'll just call it's resolved method with the value I resolved with. 我有一个延迟的承诺,但是它没有onResolved回调,因此我将使用其解析值来调用它的resolve方法。

5th Promise: The previous promise called my resolve method. 第五个承诺:先前的承诺称为我的解决方法。 I resolved and did nothing more because I have no deferred promise. 我下定决心,没有做其他事情,因为我没有延期的承诺。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM