执行解析承诺 [英] Executing Parse Promises
问题描述
我了解解析承诺(.then,.done,.when等)背后的理论,但我不知道如何执行它们.目前,我正在使用警报来迫使系统等待足够长的时间来兑现承诺,但这确实是一种很粗糙的方法.我将发布的代码完全符合我的要求.它创建一个表,从数据库中收集信息,并将其格式化为表.如果我发出警报(强制等待");但是,它仍然行不通,因为承诺尚未兑现.我应该在哪里和什么地方添加一些promise处理程序,以使其在没有警报的情况下工作?我尝试将for循环更改为do并添加了.then,但我无法使其不引发错误,但是我很确定那是因为我格式化了错误的结构.任何帮助将不胜感激.谢谢!
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 to fulfill the promises, but thats a really crude way of doing it. The code I will post does exactly what I want it to do. It creates a table, gathers info from the database, and formats it into the table. If i take out alert("forces a wait"); it wont work though, as the promise hasnt been fulfilled yet. Where and what should i add some of the promise handlers to make it work without the alert? I tried changing the for loop to a do while, and added .then but i couldnt get it to not throw errors, but im pretty sure thats because i formatted the structure wrong. Any help would be greatly appreciated. Thanks!
function billingReport(){
var sDate = new Date(document.getElementById("startDate").value);
var table = document.getElementById("results1");
var row, cell1, cell2, cell3, cell4;
var tableHeaderRowCount = 1;
var rowCount = table.rows.length;
for (var i = tableHeaderRowCount; i < rowCount; i++) {
table.deleteRow(tableHeaderRowCount);
}
Parse.Cloud.run("runReport", {sDate: sDate}, {
success: function(result){
alert("Successfully retrieved " + result.length + " scores.");
for(var i = 0; i < result.length; i++){
alert("forces a wait");
Parse.Cloud.run("caseHelper", {id: result[i].attributes.customer.id, className: "User", attribute: "username"},{
success: function(results){
row = table.insertRow(i);
cell1 = row.insertCell(0);
cell2 = row.insertCell(1);
cell3 = row.insertCell(2);
cell4 = row.insertCell(3);
cell1.innerHTML = result[i-1].id;
cell2.innerHTML = result[i-1].attributes.title;
cell3.innerHTML = result[i-1].attributes.hoursWorked;
cell4.innerHTML = results;
},
error: function(error){
alert("Error gathering customer information: " + error.code + " - " + error.message);
}
});
}
},
error: function(error){
alert("Error creating report :" + error.code + " - " + error.message);
}
});
}
这是我用于此功能的云代码:
Here is my cloud code for this function:
Parse.Cloud.define("runReport", function(request, response) {
var sDate = request.params.sDate;
var caseList = Parse.Object.extend("Cases");
var query = new Parse.Query(caseList);
query.equalTo("status", "closed");
query.greaterThanOrEqualTo("createdAt", new Date(sDate.toISOString()));
query.find({
success: function(results){
var q = results;
response.success(q);
},
error: function(error){
response.error("Failed to create query for report.");
}
})
});
Parse.Cloud.define("caseHelper", function(request, response) {
var id = request.params.id;
var className = request.params.className;
var attribute = request.params.attribute;
var list = Parse.Object.extend(String(className));
var query = new Parse.Query(String(className));
query.equalTo("objectId", id);
query.first().done(function(result){
var a = result.get(attribute);
response.success(a);
});
});
推荐答案
我可能是错的,但是Parse.Cloud.run("caseHelper",
可能是造成此问题的原因,所有这些均异步运行,而alert
的结果可能不是2 1的结果并弄乱table.insertRow(i)
,也不确定[i-1]
业务背后的原因,因为至少对于i值为0,它是行不通的.
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.
我建议您使用Promise.when
并等待所有的诺言完成之后再插入行:
I would suggest you to use Promise.when
and wait for all the promises to finish before inserting the rows:
function billingReport(){
var sDate = new Date(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.");
return Parse.Promise.when(scores.map(function(score){
return Parse.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);
});
}
这篇关于执行解析承诺的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!