javascript - Promises in Series are not getting executed in sequence -
i have written code based on parse example provided in parse.com execute promises in series.
but, seems sequential processing not working fine.
the below code calls cloud function namely 'samplecloudfuction' multiple times in sequential order through series of promises.
after executing loop, app make call js function load remaining items (excluding processed items).
this loop make multiple calls cloud function:
var seriespromise = new parse.promise.as(); $.each(items, function (i) { .. .. count++; if (count >= 25 || (i + 1) >= selecteditemslength) { .. .. //series promise: code executed in sequence being placed within //then() 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 approval state of disabled button alert("load remaining items"); });
the below function called after executing loop. but, being called in before receiving callbacks earlier requests.
seriespromise.then(function () { //fetch approval state of 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; })
that doesn't work. cannot return
callback - value vanish.
however, the docs state
every asynchronous method in parse javascript sdk returns
promise
- don't need try construct on own!
so, why sequential processing not working correctly? because requires then
callback return value of next step - can asynchronous, not yet resolved promise. then, new seriespromise
wait step before executing next.
yet not returning callback - then
resolved seriespromise
immediately undefined
. return promise .run()
yields:
var seriespromise = new parse.promise.as(); $.each(items, function (i) { … // series promise: code executed in sequence being placed within // then() existing promise seriespromise = seriespromise.then(function() { return parse.cloud.run('samplecloudfuction', itemsarray); // ^^^^^^ }); … }); seriespromise.then(function () { //fetch approval state of disabled button alert("load remaining items"); }, function (error) { alert("error " + error.code + "::"); console.log("error callback in js"); });
Comments
Post a Comment