Executing Parse Promises
I understand the theory behind the parse promises(.then, .done, .when, etc.) but I dont know how to execute them. For now I am using alerts to force the system to wait long enough
Solution 1:
I might be wrong, but Parse.Cloud.run("caseHelper",
might be causing the problem, all of these are run in async, without alert
result of 2 might come ahead of result of 1 and screw up the table.insertRow(i)
, also not sure of the reason behind [i-1]
business, because at least for i value 0, it wont work.
I would suggest you to use Promise.when
and wait for all the promises to finish before inserting the rows:
functionbillingReport(){
var sDate = newDate(document.getElementById("startDate").value);
var table = document.getElementById("results1");
var row, cell1, cell2, cell3, cell4, scores;
var tableHeaderRowCount = 1;
var rowCount = table.rows.length;
for (var i = tableHeaderRowCount; i < rowCount; i++) {
table.deleteRow(tableHeaderRowCount);
}
Parse.Cloud.run("runReport", {sDate: sDate}).then(function(data){
scores = data;
console.log("Successfully retrieved " + scores.length + " scores.");
returnParse.Promise.when(scores.map(function(score){
returnParse.Cloud.run("caseHelper", {id: score.attributes.customer.id, className: "User", attribute: "username"});
}));
}).then(function(results){
for(var i = 0; i < scores.length; i++){
row = table.insertRow(i);
cell1 = row.insertCell(0);
cell2 = row.insertCell(1);
cell3 = row.insertCell(2);
cell4 = row.insertCell(3);
cell1.innerHTML = scores[i].id;
cell2.innerHTML = scores[i].attributes.title;
cell3.innerHTML = scores[i].attributes.hoursWorked;
cell4.innerHTML = results[i];
}
}).fail(function(err){
console.log("Error creating report :" + err);
});
}
Post a Comment for "Executing Parse Promises"