蓝鸟的promise.包装方法的函数返回一个promise,可以吗? [英] bluebird promise.method to wraps a function return a promise,is that ok?

查看:84
本文介绍了蓝鸟的promise.包装方法的函数返回一个promise,可以吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在代码中使用bluebird,然后像使用api一样使用promise.method覆盖原始函数.然后,我编写了一个返回promise的函数,并使用promise.method覆盖了它.这两个函数返回了相同:

using bluebird in my code , and I use promise.method to cover a original functions just like the api say.Then I write a function which return a promise,and use promise.method to cover it.Those two function return the same:

function () {
    var ret = new Promise(INTERNAL);
    ret._captureStackTrace();
    ret._pushContext();
    var value = tryCatch(fn).apply(this, arguments);
    ret._popContext();
    ret._resolveFromSyncValue(value);
    return ret;
}

可以使用promise.method覆盖函数返回一个诺言吗?

Is it ok to use promise.method to cover the function return a promise??

推荐答案

构建带有错误的异步API

是的,Promise.method的目的是使方法安全抛出-例如,如果您有一个看起来像这样的方法:

Building async APIs with errors

Yes, the point of Promise.method is to make methods throw safe - for example if you have a method that looks like:

function foo(jsonStr){
    var obj = JSON.parse(jsonStr);
    return getApi(obj.endpoint);
}

如果传递了无效的对象,则可能会出现throw错误-因此,如果将无效的字符串传递给它,它将抛出(而不是拒绝).这意味着您必须防止在代码中引发错误和拒绝诺言,并在代码中添加} catch(e){ .catch(function(e){.

It might throw an error if passed an invalid object - so if you pass it an invalid string it'll throw (instead of reject). This means you have to guard against both thrown errors and rejected promises in your code and add a } catch(e){ and a .catch(function(e){ to your code.

这种形式的编程会创建一个弱而脆弱的API,该API区分同步错误和异步错误,并且可能(并将)根据错误发生的时间创建竞争条件. 如果函数可能是异步执行的,则必须总是异步执行.这是开发异步API的核心概念-否则最终会导致竞争条件和其他问题.

This form of programming creates a weak and fragile API that differentiates synchronous and asynchronous errors and might (and will) create race conditions based on when errors happen. If a function might do something asynchronously it must always do it asynchronously. This is a core concept of developing asynchronous APIs - otherwise you end up with race conditions and other issues.

Promise.method基本上用try/catch包裹您的方法,并将引发的错误转换为拒绝,将返回的值转换为实现.如果您的function fooPromise.method,它将始终拒绝错误而不是抛出错误.因为您的方法现在总是以相同的方式失败,所以这解决了竞争条件问题.

Promise.method basically wraps your method with a try/catch and converts thrown errors to rejections and returned values to fulfillments. If your function foo was a Promise.method it would always reject on errors instead of throwing. This fixes the race condition issue since your methods now always fail the same way.

var foo = Promise.method(function(jsonStr){
    var obj = JSON.parse(jsonStr); // if this throws - you get a rejection
    return getApi(obj.endpoint);
});

何时不使用它

在某些情况下,您可以不使用Promise.method而逃脱-例如,当方法的主体位于promise链中或您100%确信不会抛出时.总的来说-我觉得Promise.method非常有用.

When not to use it

There are cases where you can get away with not using Promise.method - for example when your method's body is in a promise chain or you're 100% sure it won't throw. In general - I find Promise.method quite useful.

它还有一个额外的好处,那就是让您从promise返回方法中返回纯值,并使它们始终如一地工作.

It also has the added benefit of letting you return plain values from promise returning methods and have them act consistently.

通常-是的.例外情况适用于YMMV.

In general - yes. Exceptions apply and YMMV.

这篇关于蓝鸟的promise.包装方法的函数返回一个promise,可以吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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