$.Deferred:如何检测每个承诺何时被执行 [英] $.Deferred: How to detect when every promise has been executed

查看:18
本文介绍了$.Deferred:如何检测每个承诺何时被执行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有许多异步任务需要完成,所以我使用了 Promise.

I have a number of async tasks that need to be completed, so I'm using promises.

我需要检测每一个承诺何时被执行(解决和拒绝).在那之前我不能继续执行.

I need to detect when each one of the promises has been executed (both resolved and rejected). I must not continue execution until that point.

我正在使用这样的东西:

I was using something like this:

$.when(promise1, promise2, ...).always();

但是这段代码是错误的,因为when 方法有惰性求值,一旦其中一个promise 失败它就会返回.因此,只要其中一个承诺失败,always 回调也会立即运行.

But this code is wrong, because the when method has lazy evaluation, and it returns as soon as one of the promises fails. So the always callback also runs as soon as one of the promises fail.

我正在考虑编写一个解决方法,但这个用例太常见了,可能有人已经这样做了,或者甚至有一种方法可以只使用 jQuery 来做到这一点(如果没有,最好添加一个 Promise.whenNonLazyPromise.when(promise1, promise2, ..., false) 将来.

I was thinking in coding a workaround, but this use case is so common that maybe somebody has done it already, or maybe there's even a way of doing this using just jQuery (if not, it would be nice to add a Promise.whenNonLazy or a Promise.when(promise1, promise2, ..., false) in the future.

这可能吗?

推荐答案

更复杂的 Promise 库有一个 allSettled() 函数,如 QPromise.settle 就像 Bluebird.

More sophisticated promise libraries have an allSettled() function like Q or Promise.settle like Bluebird.

在 jQuery 中,您也可以自己实现这样的函数并用它扩展 $ 命名空间,但这只有在您经常需要并优化性能时才有必要.

In jQuery, you could implement such a function yourself as well and extend the $ namespace with it, but that will only be necessary if you need it often and performance-optimized.

一个更简单的解决方案是为您正在等待的每个承诺创建一个新的承诺,并在底层承诺被拒绝时履行它们.然后你可以毫无问题地使用 $.when() .简而言之:

A simpler solution would be to create a new promise for each of the ones you are waiting for, and fulfilling them even when the underlying one is rejected. Then you can use $.when() on them without problems. In short:

// using Underscore's .invoke() method:
$.when.apply(null, _.invoke(promises, "then", null, $.when)).done(…)

更稳定:

$.when.apply($, $.map(promises, function(p) {
    return p.then(null, function() {
        return $.Deferred().resolveWith(this, arguments);
    });
})).then(…);

您可能会稍微更改 then 回调,以区分最终 done 中已完成和已拒绝的结果.

You might change the then callbacks a bit to distinguish between fulfilled and rejected results in the final done.

这篇关于$.Deferred:如何检测每个承诺何时被执行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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