系列中的承诺没有按顺序执行 [英] Promises in Series are not getting executed in sequence

查看:50
本文介绍了系列中的承诺没有按顺序执行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经根据 Parse.com 中提供的 Parse 示例编写了代码来执行系列中的承诺.

I have written the code based on the Parse example provided in Parse.com to execute the promises in Series.

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

But, seems like sequential processing is not working fine.

下面的代码多次调用一个云函数,即sampleCloudFuction",但通过一系列的承诺按顺序调用.

The below code calls a cloud function namely 'sampleCloudFuction' multiple times but in sequential order through Series of Promises.

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

After executing the loop, app will make a call to another js function which will load the remaining items (excluding the processed items).

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

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

            });

将在执行循环后调用下面的函数.但是,在接收所有早期请求的回调之前,它会被很好地调用.

The below function is to be called after executing the loop. But, this is being called well in before receiving the callbacks for all earlier requests.

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

        });

推荐答案

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

那行不通.您不能从回调中return - 该值将消失.

That doesn't work. You cannot return from a callback - the value will just vanish.

但是,文档指出

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

every asynchronous method in the Parse JavaScript SDK returns a Promise

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

- you don't even need to try to construct it on your own!

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

So, why is the sequential processing not working correctly? Because it requires that the then callback does return the value of the next step - which can be an asynchronous, not yet resolved promise. Then, the new seriesPromise will wait for that step before executing the next.

然而你没有从那个回调中返回任何东西——所以 then 只是用 undefined immediately 解析了 seriesPromise>.返回 .run() 产生的承诺:

Yet you were not returning anything from that callback - so the then just resolved the seriesPromise immediately with undefined. Return the promise that .run() yields:

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

这篇关于系列中的承诺没有按顺序执行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆