是否有短路Javascript承诺的好方法? [英] Is there a good way of short circuiting Javascript promises?

查看:50
本文介绍了是否有短路Javascript承诺的好方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对Promise/Deferreds有点陌生.对于成功案例和错误案例,是否有一种好的模式可以处理可能希望缩短承诺链的情况?在错误情况下,我知道您可以将.then(null, function(error) {})链接到末尾并从先前的then中捕获错误,但是如果您想以更自定义的方式处理错误并终止该怎么办?您是否会在早期的错误处理程序中指定错误的类型",并通过新的承诺将其返回,以在最终的错误处理程序中进行处理或跳过?还有一个成功案例,您想在链中更早地终止(仅有条件地触发任何以后的then)?

I'm a bit of a novice with promises/Deferreds. Is there a good pattern to handle the case where one might want to short circuit a chain of promises, for both success and error cases? In the error situation, I know you can chain a .then(null, function(error) {}) to the end and catch an error from any of the previous thens, but what if you want to handle an error in a more custom way and terminate? Would you specify a 'type' of error in an earlier error handler and return it via a new promise, to be handled or skipped in the final error handler? And what about a success case, where you want to terminate earlier in the chain (only conditionally firing off any later then's)?

推荐答案

通常,promise链始于对某些异步函数的调用,例如:

Typically, the promise chain starts with a call to some asynchronous function as such:

var promise = callAsync();

如果要链接第二个异步调用,则可能会执行以下操作:

If you are chaining a second async call, you probably do something like this:

var promise = callAsync()
.then(function(){
    return callOtherAsync();
})
.then(function(){
    return callSuccessAsync();
}, function(){
    return callFailAsync();
});

作为链接的结果,promise现在包含最终的承诺,该承诺在callFinalAsync()的承诺完成时完成.使用此模式时,无法短路最终的promise-您可以在执行过程中返回失败的承诺(例如,而不是返回callOtherAsync的结果),但这需要失败的承诺才能在链中进行(从而导致调用callFailAsync). 您总是可以像这样在回调中实现或拒绝promise

As a result of chaining, promise now contains the final promise which completes when callFinalAsync()'s promise completes. There is no way to short circuit the final promise when using this pattern - you can return a failed promise along the way (for instance, rather than returning the result of callOtherAsync) but that requires the failed promise to progress through the chain (thus causing callFailAsync to be called). You can always fulfill or reject the promise from within the callbacks as such

var promise = callAsync()
.then(function(){
    if(fail){
        promise.reject();
        //no way to halt progression 
    }else{
        return callOtherAsync();
    }
})
.then(function(){
    return callSuccessAsync();
}, function(){
    return callFailAsync();
});

但是,这不会阻止调用callFailAsync().为此,一些Promise/A实现公开了stop方法.停止后,您可以执行以下操作:

however, this will not prevent calls to callFailAsync(). Some Promise/A implementations expose a stop method for just this purpose. With stop, you could do this:

var promise = callAsync();
.then(function(){
    if(fail){
        this.stop(); 
        promise.reject();
    }else{
        return callOtherAsync();
    }
})
.then(function(){
    return callSuccessAsync();
}, function(){
    return callFailAsync();
});

这取决于可以使用this访问中间承诺.一些Promise实现禁止这样做(强制this为window/null/etc),但是您可以使用闭包来处理它.

Which depends on having access to the intermediate promise with this. Some Promise implementations forbid that (forcing this to be window/null/etc), but you can deal with that with a closure.

TL; DR:Promise/A规范不提供链状短路功能,但添加一个并不难.

TL;DR: Promise/A spec doesn't provide a chain short circuit function, but it's not hard to add one.

这篇关于是否有短路Javascript承诺的好方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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