需要jQuery's "when"mpromise/mongoose的功能

Need jQuery's "when" functionality for mpromise/mongoose

本文关键字:quot mongoose 功能 mpromise when jQuery 需要      更新时间:2023-09-26

是否存在不承诺/猫鼬的when条款?我希望做这样的事情,而不必为mpromise编写自己的包装器。

$.when(jQueryPromise1,jQueryPromise3,jQueryPromise3).done(function(r1,r2,r3) {
    // success code
}.fail(function(err1,err2,err3) {
    //failure code
});

我意识到锁链的存在,那不是我想要的。我正在寻找一个机制,在不承诺/猫鼬,将执行当所有的承诺已经完成。

下面是when的示例实现:

function when(/* promise list */) {
    var promises = [].slice.call(arguments),
        whenPromise = new Promise,
        results = new Array(promises.length),
        remaining = promises.length,
        done = false,
        finish = function() {
            done = true;
        };
    whenPromise.onFulfill(finish).onReject(finish);
    promises.forEach(function(promise) {
        promise.onFulfill(function(result) {
            if (!done) {
                // index of result should correspond to original index of promise
                results[promises.indexOf(promise)] = result;
                if (--remaining == 0) {
                    // fulfill when all are fulfilled
                    whenPromise.fulfill.apply(whenPromise, results);
                }
            }
        }).onReject(function(err) {
            if (!done) {
                // reject when one is rejected (a la jQuery)
                whenPromise.reject(err);
            }
        });
    });
}