尝试兑现承诺 [英] Try and catch around promise

查看:66
本文介绍了尝试兑现承诺的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要编写一个返回Promise的函数,首先我调用一个同步函数A(),该函数返回一些结果. 然后返回一个函数B(result),其中B是一个承诺,它接受A()的结果. 如果任何一个函数失败,我都希望在C是一个诺言的情况下调用相同的错误函数C(error). 编写此内容的最佳方法是什么.这就是我所拥有的,但我认为有一种明显的失踪方式

I need to write a function that returns a promise, where first I call a synchronous function A() which returns some result. Then return a function B(result) where B is a promise which takes in the result of A(). If either function fails I want the same error function C(error) to get called where C is a promise. What is the best way of writing this. This is what I have but think there is obvious way I am missing

function() {
    try {
        var result = A();
        return B(result)
            .catch(function(error) {
                return C(error);
            });
     }
     catch(error) {
         return C(error);
     }
}

将同步try and catch与promise .catch结合起来似乎是错误的,而且错误,我需要在两个不同的地方调用C(error).

It seems wrong combining synchronous try and catch with a promise .catch and also wrong there are two different places I need to call C(error).

A()引发错误而不是返回错误代码.

A() throws an error rather than returning an error code.

推荐答案

您并没有确切说明A()如何失败.它可能会抛出,也可能返回错误结果.我将为两者展示一个方案.同步和异步混合的关键是始终返回承诺.无论函数成功与否,这都将为调用者提供一个一致的接口.

You don't say exactly how A() fails. It could either throw or it could return an error result. I'll show a scheme for both. The key to a mix of sync and async is to always return a promise. This will give you a consistent interface for teh caller no matter how the function succeeds or fails.

如果仅担心A()引发异常并且它不返回错误代码,则可以执行以下操作:

If you are only worried about A() throwing an exception and it doesn't return an error code, then you can do this:

function someFunction() {
    try {
        var result = A();
        return B(result);
     } catch(err) {
         return Promise.reject(err);
     }
}

someFunction().then(function(result) {
    // code here to process the final result
}).catch(C);

如果还存在A()可以返回错误代码的情况,则可以执行以下操作:

If you also have the case where A() can return an error code, then you can do this:

function someFunction() {
    try {
        var result = A();
        // check for error value
        if (result < 0) {
            throw result;
        }
        return B(result);
     } catch(err) {
         return Promise.resolve(err);
     }
}

请注意,这两种模式都避免了在不需要时产生额外的承诺.仅当返回同步发生的错误时,它们才会创建额外的promise.

Note that both of these patterns avoid creating an extra promise if it isn't needed. They only create the extra promise when returning an error that occurred synchronously.

蓝鸟诺言库在这种特殊情况下具有帮助功能,称为Promise.method. Promise.method()的实用程序是它会自动将您的函数包装在try/catch处理程序中,并且如果引发任何同步异常,它将自动将它们转换为返回被拒绝的承诺.您可以这样使用它:

The Bluebird promise library has a helper function for this particular circumstance called Promise.method. The utility of Promise.method() is that it automatically wraps your function in a try/catch handler and if there are any synchronous exceptions thrown, it automatically turns them into returning a rejected promise. You could use it like this:

var someFunction = Promise.method(function() {
    var result = A();
    // check for error condition
    if (result < 0) {
        throw result;
    }
    return B(result);
});

someFunction().then(function(result) {
    // code here to process the final result
}).catch(C);

这篇关于尝试兑现承诺的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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