如何正确使用解析/承诺? [英] How to properly use Parse / Promise?
问题描述
我正在使用Parse.com编写一些JavaScript代码.
I am writing some JavaScript codes using Parse.com.
说实话,我一直在阅读如何使用Promise并进行了大量研究,但仍无法弄清楚如何正确使用它..
To be honest, I have been reading how to use Promise and done lots of research but cannot still figure out how to use it properly..
这是一个场景:
- 我有两个表(对象),分别称为 Client 和 InvoiceHeader
- 客户可以有多个 InvoiceHeader .
- InvoiceHeader 有一列,称为金额",我希望每个客户的 InvoiceHeader 总数.
- I have two tables (objects) called Client and InvoiceHeader
- Client can have multiple InvoiceHeaders.
- InvoiceHeader has a column called "Amount" and I want a total amount of each client's InvoiceHeaders.
例如,如果客户端A有两个金额分别为30和20的 InvoiceHeader ,而客户端B没有任何内容,那么我想在tempArray中看到的结果是"50,0".
For example, if Client A has two InvoiceHeaders with amount 30 and 20 and Client B has got nothing, the result I want to see in tempArray is '50, 0'.
但是,使用以下代码,看起来像是随机的.我的意思是有时tempArray
的值为'50,50'或"50,0".我怀疑这是由于Promise的错误使用造成的.
However, with the following codes, it looks like it's random. I mean sometimes the tempArray
got '50, 50' or "50, 0". I suspect it is due to the wrong usage of Promise.
请帮助我.我一直在研究代码,并停留了几天.
Please help me. I have been looking into the codes and stuck for a few days.
$(document).ready(function() {
var client = Parse.Object.extend("Client");
var query = new Parse.Query(client);
var tempArray = [];
query.find().then(function(objects) {
return objects;
}).then(function (objects) {
var promises = [];
var totalForHeader = 0;
objects.forEach(function(object) {
totalForHeader = 0;
var invoiceHeader = Parse.Object.extend('InvoiceHeader');
var queryForInvoiceHeader = new Parse.Query(invoiceHeader);
queryForInvoiceHeader.equalTo('headerClient', object);
var prom = queryForInvoiceHeader.find().then(function(headers) {
headers.forEach(function(header) {
totalForHeader += totalForHeader +
parseFloat(header.get('headerOutstandingAmount'));
});
tempArray.push(totalForHeader);
});
promises.push(prom);
});
return Parse.Promise.when.apply(Parse.Promise, promises);
}).then(function () {
// after all of above jobs are done, do something here...
});
} );
推荐答案
假定Parse.com的Promise
类遵循A +规范,并且我知道您想在哪位结束,这应该可行:
Assuming Parse.com's Promise
class follows the A+ spec, and I understood which bits you wanted to end up where, this ought to work:
$(document).ready(function() {
var clientClass = Parse.Object.extend("Client");
var clientQuery = new Parse.Query(clientClass);
clientQuery.find().then(function(clients) {
var totalPromises = [];
clients.forEach(function(client) {
var invoiceHeaderClass = Parse.Object.extend('InvoiceHeader');
var invoiceHeaderQuery = new Parse.Query(invoiceHeaderClass);
invoiceHeaderQuery.equalTo('headerClient', client);
var totalPromise = invoiceHeaderQuery.find().then(function(invoiceHeaders) {
var totalForHeader = 0;
invoiceHeaders.forEach(function(invoiceHeader) {
totalForHeader += parseFloat(invoiceHeader.get('headerOutstandingAmount'));
});
return totalForHeader;
});
totalPromises.push(totalPromise);
});
return Parse.Promise.when(totalPromises);
}).then(function(totals) {
// here you can use the `totals` array.
});
});
这篇关于如何正确使用解析/承诺?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!