【问题标题】:How to determine if Javascript Q deferred() has executed all the functions in the chain如何判断 Javascript Q deferred() 是否已经执行了链中的所有函数
【发布时间】:2014-04-29 07:59:42
【问题描述】:

有没有可能知道,当使用 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 库中获得某种回调或通知,本质上意味着队列是“空的”并且所有注册/执行延迟函数?

提前致谢。

【问题讨论】:

    标签: javascript promise deferred q


    【解决方案1】:

    是的,有!哎呀-我什至可以说这是承诺的全部意义。

    那个方法叫做 drumroll .then!

    在使用它时,我们还将使用Q.all 来等待一组promise。这是因为你有一个循环。如果你没有 - 我们只需要.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
     });
    

    如果你从这篇文章中得到一件事 - 它应该是从方法中返回承诺 :)。此外,不是运行 myCb 而是简单地返回值并从外部运行 myCb - 它更强大:)

    【讨论】:

    • 谢谢。这就像一个魅力:)。那么,你能告诉我,最后一次回调中的“结果”是什么?这里。 testJSCallbacks().then(function(results){ // everything is complete and none of it rejected }); 是的,我现在知道了,你可以返回 Promise 形式的方法。我只知道 Q 一天,并且已经爱上了它 :)。
    • 它包含一个数组,其中每个项目是数组中一个动作的返回值(5 个动作)
    猜你喜欢
    • 2012-05-09
    • 2010-09-10
    • 1970-01-01
    • 2012-05-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多