如何使用JS承诺来捕获异步错误? [英] How can I catch an asynchronous error using JS promises?

查看:187
本文介绍了如何使用JS承诺来捕获异步错误?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以使用ES6 .catch 承诺的语法来捕获异步错误?例如,以下内容不起作用(.catch没有捕获错误):

 新的Promise((resolve ,reject)=> {
setTimeout(()=> {throw new Error(uh oh)},1);
})然后(number => {
console.log(Number:+ number);
})。catch(e => {
console.log(Error:+ e);
});

但是这个同步版本是:

  new Promise((resolve,reject)=> {
throw new Error(uh oh);
})然后(number => {
console.log(Number:+ number);
})。catch(e => {
console.log(Error:+ e);
} );

是唯一的解决方案,使用try / catch块和拒绝收到错误?

 新的承诺((解决,拒绝) = {
try {
setTimeout(()=> {throw new Error(uh oh)},1);
}
catch(e)
reject(e);
}
})然后(number => {
console.log(Number:+ number);
}) .catch(e => {
console.log(Error:+ e);
});

为了这个问题,假设抛出错误的代码部分在另一个命名函数,因此它无法访问拒绝函数。



谢谢!!



编辑:这是一个更完整的我想要的例子要做,在JSFiddle。

解决方案

使用 resolve() reject() Promise 构造函数中。在 onRejected .catch()之间处理错误。注意,一旦错误被处理, onFulfilled 链接 .then()(如果有),除非 throw onRejected .catch(),将错误显式传递给链接的 .then(_,onRejected) .catch )



  function fn(){throw new错误(uh oh)} new Promise((resolve,reject)=> {setTimeout(()=> {try {resolve(fn())} catch(e){reject(e)}},1 );})然后(number => {console.log(Number:+ number);},e => {console.log(Error:+ e);});  


Is it possible to catch asynchronous errors using the ES6 .catch syntax of promises? For example, the following doesn't work (the .catch doesn't catch the error):

new Promise((resolve, reject)=>{
    setTimeout(()=>{throw new Error("uh oh")}, 1);
}).then(number=>{
    console.log("Number: " + number);
}).catch(e=>{
    console.log("Error: " + e);
});

But this synchronous version does:

new Promise((resolve, reject)=>{
    throw new Error("uh oh");
}).then(number=>{
    console.log("Number: " + number);
}).catch(e=>{
    console.log("Error: " + e);
});

Is the only solution to do something like the following, using a try/catch block and rejecting the error in the catch?

new Promise((resolve, reject)=>{
    try {
        setTimeout(()=>{throw new Error("uh oh")}, 1);
    }
    catch(e) {
        reject(e);
    }
}).then(number=>{
    console.log("Number: " + number);
}).catch(e=>{
    console.log("Error: " + e);
});

For the sake of this question, assume the part of the code that is throwing the Error is in another named function, so it doesn't have access to the reject function.

Thanks!!

Edit: Here is a more complete example of what I'd like to do, in JSFiddle.

解决方案

Use resolve(), reject() within Promise constructor. Handle error at either onRejected or .catch().

Note, once error is handled, onFulfilled at chained .then(), if any, should be reached, unless throw is used within onRejected or .catch(), to explicitly pass error to chained .then(_, onRejected) or .catch()

function fn() {
  throw new Error("uh oh")
}

new Promise((resolve, reject) => {
  setTimeout(() => {
    try {
      resolve(fn())
    } catch (e) {
      reject(e)
    }
  }, 1);
}).then(number => {
  console.log("Number: " + number);
}, e => {
  console.log("Error: " + e);
});

这篇关于如何使用JS承诺来捕获异步错误?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆