我如何处理 promise 的错误? [英] How do I handle errors with promises?

查看:13
本文介绍了我如何处理 promise 的错误?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

作为节点程序员.我习惯于使用nodebacks"来处理我的代码中的错误:

As a node programmer. I'm used to use "nodebacks" for handling errors in my code:

myFn(param, function(err, data) {
    if (err){
        //error handling logic
     }
     else {
        // business logic
    }
});

在编写该函数时,我可以执行以下操作:

When writing that function, I can do something like:

var myFn = function(param, callback){
    var calc = doSomeCalculation(param);
    if(calc === null) { // or some other way to detect error
        callback(new Error("error with calculation"), null);
    }
    ...
    someAsyncOp(calcN,function(err, finalResult){
        if(err) return callback(err, null);
        callback(null, finalResult); // the error is null to signal no error
    });

};

我将如何使用 Promise 进行这种错误处理?

How would I do this sort of error handling with promises?

推荐答案

经验法则

每当您对如何使用 Promise 做某事有疑问时,请考虑同步版本.

Rule of Thumb

Whenever you have a doubt about how to do something with promises - think about the synchronous version.

try{
   var result = myFn(param);
   // business logic with result
} catch(e) {
    //error handling logic
}

至少在我看来,这比第一个参数有时为 null 的回调要简洁得多.

This, at least to me looks a lot cleaner than a callback with a first parameter that is sometimes null.

promises 方式几乎总是与问题的同步版本非常相似:

The promises way is almost always very similar to the synchronous version of the problem:

myFn(param).then(function(result){
    // business logic with result
}).catch(function(e){
    //error handling logic
});

处理回调时 myFn 的样子:

Where myFn would look something like when working with callbacks:

var myFn = function(param){
    return new Promise(function(resolve, reject){
        var calc = doSomeCalculation(param);
        if(calc === null) { // or some other way to detect error
            reject(new Error("error with calculation"), null);
        }
        someAsyncOp(calcN,function(err, finalResult){
            if(err) reject(err);
            resolve(finalResult);
        })
    });
};

使用回调/nodebacks

这只是你在使用回调时应该做的事情,当使用承诺时它要简单得多,你可以这样做:

Working with callbacks/nodebacks

This is only something you should have to do when working with callbacks, when working with promises it is a lot simpler, and you can do:

var myFn = function(param){
    var calc = doSomeCalculation(param);
    ...
    return someAsyncOp(calcN); // returning a promise.
}

此外,在 promise 链中工作时,您可以获得安全性:

Moreover, when working inside promise chains, you get throw safety:

myFn(param).then(function(calcN){
   // here, you throw to raise an error and return to resolve
   // new Promise should be used only when starting a chain.
}).catch(function(err){
    // handle error
}).then(function(){
   // ready to go again, we're out of the catch
});

<小时>

请注意,一些库,例如 BluebirdRSVPQ 提供语法糖和方法的自动promisification,因此您很少需要自己使用new Promise.

另外,考虑阅读this 以了解有关承诺错误处理的更多信息.

Also, consider reading this and that to learn more about promise error handling.

这篇关于我如何处理 promise 的错误?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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