【问题标题】:Promises in Series are not getting executed in sequence系列中的承诺没有按顺序执行
【发布时间】:2014-03-06 21:13:19
【问题描述】:

我已经根据 Parse.com 中提供的 Parse 示例编写了代码来执行 Series 中的 Promise。

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

以下代码多次调用名为“sampleCloudFuction”的云函数,但通过一系列承诺按顺序调用。

执行完循环后,app会调用另一个js函数来加载剩余的item(不包括处理过的item)。

这是多次调用云函数的循环:

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");

        });

【问题讨论】:

  • 您需要显示定义 seriesPromise 的代码
  • @wayne,我在上面的代码中加入了注释。 -----var seriesPromise = new Parse.Promise.as();--

标签: javascript parse-platform promise


【解决方案1】:
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 只是用undefined 解决了seriesPromise 立即。返回.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");
});

【讨论】:

    【解决方案2】:
    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。

    【讨论】:

      猜你喜欢
      • 2013-12-04
      • 2018-09-03
      • 1970-01-01
      • 2020-08-23
      • 1970-01-01
      • 1970-01-01
      • 2019-05-30
      • 2017-07-14
      • 1970-01-01
      相关资源
      最近更新 更多