Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/413.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 一系列的承诺没有按顺序执行_Javascript_Parse Platform_Promise - Fatal编程技术网

Javascript 一系列的承诺没有按顺序执行

Javascript 一系列的承诺没有按顺序执行,javascript,parse-platform,promise,Javascript,Parse Platform,Promise,我已经基于Parse.com中提供的Parse示例编写了代码,以执行系列承诺 但是,似乎顺序处理并不能很好地工作 下面的代码多次调用一个云函数,即“SampleCloudFunction”,但顺序是通过一系列承诺 执行循环后,应用程序将调用另一个js函数,该函数将加载剩余的项(不包括已处理的项) 这是对云函数进行多次调用的循环: var seriesPromise = new Parse.Promise.as(); $.each(items, function (i) {

我已经基于Parse.com中提供的Parse示例编写了代码,以执行系列承诺

但是,似乎顺序处理并不能很好地工作

下面的代码多次调用一个云函数,即“SampleCloudFunction”,但顺序是通过一系列承诺

执行循环后,应用程序将调用另一个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");

        });
不幸的是,我真的不明白你想做什么。如果您的代码太长,您可以放置一个JSFIDLE

Parse.Cloud.run('sampleCloudFuction', itemsArray, function () {
    console.log("success callback in JS");
    var tempPromise = Parse.Promise.as();
    return tempPromise;
})
那不行。您不能从回调中
返回
——该值将消失

但是,声明

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

-你甚至不需要自己去建造它

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

但是您没有从该回调返回任何内容-因此,
然后
只是用
未定义的
立即解决了
系列问题。返回
.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");
});

您需要显示seriesPromise所在位置的代码define@wayne,我已包含对上述代码的注释-----var seriesPromise=new Parse.Promise.as()--
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");
});