jQuery $ .wait()用于*全部*被推迟吗? [英] jQuery $.wait() for *all* deferred's to be rejected?

查看:122
本文介绍了jQuery $ .wait()用于*全部*被推迟吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

jQuery具有Deferred API的一个不错的功能,$.wait()用于处理多个Deferred s/Promise s.它在以下情况下返回:

jQuery has a nice feature of its Deferred API, $.wait() for working with multiple Deferreds / Promises. It returns when:

  • 所有 resolve() d
  • All of the Deferreds have been resolve()d

  • Deferred个中的一个已被reject()
  • One of the Deferreds has been reject()ed

大多数情况下这是您想要的,但是有时您想知道所有何时完成.

Most of the time this is what you want, but sometimes you want to know when all of them have been reject()ed.

是否有一种简单或优雅的方法来执行类似$.wait()的操作,但是仅当所有Deferred被拒绝时?

Is there a simple or elegant way to do something like $.wait() but only when all Deferreds have been rejected?

(可能还有其他用例,但我的是将其与

(There may be other use cases but mine is to combine this with waiting for the first of several Deferred's to resolve.)

推荐答案

根据Promise规范如何使用PromiseInspection对象在未来发展的精神,这是一个jQuery附加功能,可告诉您所有诺言已兑现,无论是兑现还是拒绝:

In the spirit of how the Promise specification is likely going for the future with a PromiseInspection object, here's a jQuery add-on function that tells you when all promises are done, whether fulfilled or rejected:

// pass either multiple promises as separate arguments or an array of promises
$.settle = function(p1) {
    var args;
    if (Array.isArray(p1)) {
          args = p1;
    } else {
        args = Array.prototype.slice.call(arguments);
    }

    function PromiseInspection(fulfilled, val) {
        return {
            isFulfilled: function() {
                return fulfilled;
            }, value: function() {
                return fulfilled ? val: undefined;
            }, reason: function() {
                return !fulfilled ? val: undefined;
            }
        };
    }
    return $.when.apply($, args.map(function(p) {
        // if incoming value, not a promise, then wrap it in a promise
        if (!p || (!(typeof p === "object" || typeof p === "function")) || typeof p.then !== "function") {
            p = $.Deferred().resolve(p);
        }
        // Now we know for sure that p is a promise
        // Make sure that the returned promise here is always resolved with a PromiseInspection object, never rejected
        return p.then(function(val) {
            return new PromiseInspection(true, val);
        }, function(reason) {
            // convert rejected promise into resolved promise
            // this is required in jQuery 1.x and 2.x (works in jQuery 3.x, but the extra .resolve() is not required in 3.x)
            return $.Deferred().resolve(new PromiseInspection(false, reason));
        });
    })).then(function() {
          // return an array of results which is just more convenient to work with
          // than the separate arguments that $.when() would normally return
        return Array.prototype.slice.call(arguments);
    });
}

然后,您可以像这样使用它:

Then, you can use it like this:

$.settle(promiseArray).then(function(inspectionArray) {
    inspectionArray.forEach(function(pi) {
        if (pi.isFulfilled()) {
            // pi.value() is the value of the fulfilled promise
        } else {
            // pi.reason() is the reason for the rejection
        }
    });
});

请记住,$.settle()将始终满足(从不拒绝),并且实现的值是由PromiseInspection对象组成的数组,您可以询问每个对象以查看其是否被实现或拒绝,然后获取相应的值或原因.有关用法示例,请参见下面的演示:

Keep in mind that $.settle() will always fulfill (never reject) and the fulfilled value is an array of PromiseInspection objects and you can interrogate each one to see if it was fulfilled or rejected and then fetch the corresponding value or reason. See the demo below for example usage:

正在运行的演示: https://jsfiddle.net/jfriend00/y0gjs31r/

这篇关于jQuery $ .wait()用于*全部*被推迟吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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