什么时候异步方法抛出,你如何抓住他们? [英] When do async methods throw and how do you catch them?

查看:181
本文介绍了什么时候异步方法抛出,你如何抓住他们?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

从节点 doc



< blockquote>

Node.js API中的一些典型的异步方法可能是
仍然使用throw机制来引发必须使用
处理的异常试着抓。没有这种方法的全面清单;
请参考每种方法的文档,以确定所需的
适当的错误处理机制。


有人可以这样的功能的例子是异步还是抛出?如何和什么时候捕获例外?



更具体地说。他们是否参考这样的功能:

  try 
{

obj.someAsync( param,function(data){
console.log(data);
});
} catch(e)
{

}

现在通常我知道上面没有意义 - 因为当回调执行时,尝试阻止可能已经退出。




  • 但是文档摘录的例子是什么样的?如果异步方法抛出它们所说的话,那么应该在哪里,何时以及如何处理它? (或者如果你显示这样的功能,你可以在文档中显示它在报价中提到的处理方式吗?)


解决方案

像您的示例中的异步方法通常会导致程序员错误,如错误的参数,并且他们调用回调错误的操作错误。


$ b $但是在ES2017中也有异步函数(用 async函数声明),那些信号错误通过拒绝它们返回的承诺 - 这变成抛出异常您可以使用等待关键字。



示例:

  function x(arg,cb){
if(!arg)throw new Error('Programmer error:bad arguments');
setTimeout(()=> {
cb(new Error('Operational error:something bad happen'));
},2000);
}

现在,当您使用它时,通常不要想处理程序员的错误 - 你想修复它们。所以你不这样做:

  try {
x();
} catch(err){
//为什么我应该处理坏调用的情况
//而不是修复它?
}

您处理的操作错误如下:

  x(function(err){
if(err){
// handle error
} else {
// success
}
});

现在,如果您有一个不接受回调但返回承诺的函数: p>

  function y(){
return new Promise((res,rej)=> setTimeout(()=> rej('error'),2000));
}

然后你处理这样的错误:

  y()。catch(error => {
// handle error
});

或使用等待 p>

  try {
await y();
} catch(err){
// handle error
}

有关操作错误和程序员错误差异的更多信息,请参阅:





有关承诺的更多信息和 async / await 请参阅此答案


From node doc:

A handful of typically asynchronous methods in the Node.js API may still use the throw mechanism to raise exceptions that must be handled using try / catch. There is no comprehensive list of such methods; please refer to the documentation of each method to determine the appropriate error handling mechanism required.

Can someone bring example of such function which is async and still throws? How and when do you catch the exception then?

More particularly. Do they refer to such function:

try
{

   obj.someAsync("param", function(data){
                    console.log(data);
                 });
}catch(e)
{

}

Now normally I know above doesn't make sense -because when the callback executes, try block could have been already exited.

  • But what kind of example does the excerpt from documentation refer to? If async method throws as they say it, where, when and how should I handle it? (or maybe if you show such function can you show where in its doc it says how to handle it as mentioned on the quote?)

解决方案

The async methods like the one from your example usually throw for programmer errors like bad parameters and they call the callback with error for operational errors.

But there are also async functions in ES2017 (declared with async function) and those signal errors by rejecting the promise that they return - which turn into a thrown exception when you use them with await keyword.

Examples:

function x(arg, cb) {
    if (!arg) throw new Error('Programmer error: bad arguments');
    setTimeout(() => {
        cb(new Error('Operational error: something bad happened'));
    }, 2000);
}

Now when you use it you usually don't want to handle programmer errors - you want to fix them. So you don't do this:

try {
    x();
} catch (err) {
    // Why should I handle the case of bad invocation
    // instead of fixing it?
}

And the operational errors you handle like this:

x(function (err) {
    if (err) {
        // handle error
    } else {
        // success
    }
});

Now, if you have a function that doesn't take a callback but returns a promise:

function y() {
    return new Promise((res, rej) => setTimeout(() => rej('error'), 2000));
}

Then you handle the error like this:

y().catch(error => {
    // handle error
});

or, while using await:

try {
    await y();
} catch (err) {
    // handle error
}

For more info on the difference between operational errors and programmer errors see:

For more info on the promises and async/await see the links in this answer.

这篇关于什么时候异步方法抛出,你如何抓住他们?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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