系列中的承诺不会按顺序执行

时间:2014-03-06 11:12:59

标签: javascript parse-platform promise

我已根据Parse.com提供的Parse示例编写代码,以执行系列中的承诺。

但是,似乎顺序处理工作不正常。

下面的代码多次调用云函数即“sampleCloudFuction”,但是按照Promise系列的顺序调用。

执行循环后,app将调用另一个js函数,该函数将加载剩余的项目(不包括已处理的项目)。

这是循环,可以多次调用云函数:

var seriesPromise = new Parse.Promise.as();

 $.each(items, function (i) {
                ..
                ..

                count++;
                if (count >= 25 || (i + 1) >= selectedItemsLength) {

                    .. ..

//Series Promise: The code to be executed in sequence being placed within the 
//then() the existing promise

                    seriesPromise = seriesPromise.then(function () {
                        Parse.Cloud.run('sampleCloudFuction', itemsArray, function () {
                            console.log("success callback in JS");
                            var tempPromise = Parse.Promise.as();
                            return tempPromise;


                        }, function (error) {
                            alert("Error " + error.code + "::");
                            console.log("error callback in JS")
                        });
                    });

                    count = 0;


                }
            });

..
..


seriesPromise.then(function () {
                //Fetch the approval state of the disabled button
                alert("load remaining items");

            });

执行循环后将调用以下函数。但是,在收到所有早期请求的回调之前,这个函数已被调用。

seriesPromise.then(function () {
            //Fetch the approval state of the disabled button
            alert("load remaining items");

        });

2 个答案:

答案 0 :(得分:2)

Parse.Cloud.run('sampleCloudFuction', itemsArray, function () {
    console.log("success callback in JS");
    var tempPromise = Parse.Promise.as();
    return tempPromise;
})

这不起作用。你不能从回调中return - 价值就会消失。

但是,the docs表示

  

Parse JavaScript SDK中的每个异步方法都返回Promise

- 您甚至不需要尝试自己构建它!

那么,为什么顺序处理不能正常工作?因为它要求then回调确实返回下一步的值 - 这可能是异步的,尚未解决的承诺。然后,新seriesPromise将在执行下一步之前等待该步骤。

然而,您没有从该回调中返回任何内容 - 因此then刚刚使用seriesPromise解决了undefined 。返回.run()产生的承诺:

var seriesPromise = new Parse.Promise.as();
$.each(items, function (i) {
    …
    // Series Promise: The code to be executed in sequence being placed within the 
    // then() the existing promise
    seriesPromise = seriesPromise.then(function() {
       return Parse.Cloud.run('sampleCloudFuction', itemsArray);
//     ^^^^^^
    });
    …
});
seriesPromise.then(function () {
    //Fetch the approval state of the disabled button
    alert("load remaining items");
}, function (error) {
    alert("Error " + error.code + "::");
    console.log("error callback in JS");
});

答案 1 :(得分:0)

var seriesPromise = new Parse.Promise.as();

 $.each(items, function (i) {
                ..
                ..

                count++;
                if (count >= 25 || (i + 1) >= selectedItemsLength) {

                    .. ..

                // I don't know where you got the understand wrapping code in 
                // Parse.Promise.as() it will executed in series. 

                // from the docs:
                // Parse.Promise.as()
                // Returns a new promise that is resolved with a given value.

                // in fact the following line executed right away and this foreach 
                // function for this item return immediately.
                    seriesPromise = seriesPromise.then(function () {
                        Parse.Cloud.run('sampleCloudFuction', itemsArray, function () {
                            console.log("success callback in JS");
                            var tempPromise = Parse.Promise.as();
                            return tempPromise;


                        }, function (error) {
                            alert("Error " + error.code + "::");
                            console.log("error callback in JS")
                        });
                    });

                    count = 0;


                }
            });

不幸的是,我真的不明白你想做什么。如果你的代码太长,你可以放一个jsfiddle。