如何确定Javascript Q deferred()是否已执行链中的所有函数

时间:2014-03-21 19:05:15

标签: javascript promise deferred q

有可能知道,当使用Javascript Q promise库执行链中注册的所有功能时?

我会发布here的示例代码(实际上我的问题是跟进的):

testJSCallbacks();

function testJSCallbacks(){
    var i = 0,
        promise;

    for (i = 0; i < 5; i++) {
        //Make initial promise if one doesn't exist
        if (!promise) {
            promise = Q.fcall(getStep(i));
        }
        //Append to existing promise chain
        else {
            promise = promise.then(getStep(i));
        }
        //then function returns another promise that can be used for chaining.
        //We are essentially chaining each function together here in the loop.
        promise = promise.then(function (key) {
            //Log the output of step here
            console.log("Step 1 " + key);
            return key;
        })
        //then function takes a callback function with one parammeter (the data).
        //foo signature meets this criteria and will use the resolution of the last promise (key).
        .then(foo)
        //myCB will execute after foo resolves its promise, which it does in the onsuccess callback
        .then(myCB);
    }
}

function getStep(step) {
    return function () {
        return step;
    }
}

function foo(key) {
    //retrieve png image blob from indexedDB for the key 'key'. Assume that the database is
    //created and started properly
    var getRequest = transaction.objectStore("store").get(key),
        //Need to return a promise
        deferred = Q.defer();
    getRequest.onsuccess = function (event) {
        var result = event.target.result;
        if(result){
            console.log("Step 2 " + key + " Found");
        }else{
            console.log("Step 2 " + key + " not Found");  
        }
        deferred.resolve(result);
    }

    return deferred.promise;
}

function myCB (result){
    console.log("Step 3: " + result);
}

=============

如果您在代码中注意到foo()和myCB()都将执行5次。

我想知道的是,当上次执行函数myCB()时,是否有可能从Q库中获取callBack或某种类型的通知,这实际上意味着队列为“空”且所有已注册/执行延期函数?

提前致谢。

1 个答案:

答案 0 :(得分:1)

是的,有!哎呀 - 我甚至可以说这是承诺的全部要点。

该方法名为 drumroll .then

使用它时,我们还会使用Q.all来等待承诺的集合。这是因为你有一个循环。如果你没有 - 我们所需要的只是.then

function testJSCallbacks(){
       var promises = [];
       for(var i = 0; i < 5; i++){
       ...
       promise = promise.then(function (key) {
            //Log the output of step here
            console.log("Step 1 " + key);
            return key;
        })
        //then function takes a callback function with one parammeter (the data).
        //foo signature meets this criteria and will use the resolution of the last promise (key).
        .then(foo)
        //myCB will execute after foo resolves its promise, which it does in the onsuccess callback
        .then(myCB);
        promises[i] = promise;
    }
    return Q.all(promises); // note the return statement
}

然后,你可以勾选它的完成:

 testJSCallbacks().then(function(results){
        // everything is complete and none of it rejected
 });

如果你从这篇文章中拿出一件事 - 它应该是从方法中返回promises :)。此外,不是运行myCb而只是返回值并从外部运行myCb - 它更强大:)