我需要在 ionic 中使用以下 promise (ios 显然在 android 上没问题):
var Attendees = Parse.Object.extend("Attendees");
return Promise.all(eventDetails.map(detail => {
return Promise.all([new Promise((res, rej) => {
var query2 = new Parse.Query(Attendees);
query2.equalTo("event_id", detail .id_event);
query2.count({
success: function(number) {
detail["n_requests_received"] = number;
alert("received")
res('Some value if required'); //These are needed or promise chain will hang
}
});
}),
new Promise((res, rej) => {
var query3 = new Parse.Query(Attendees);
query3.equalTo("event_id", detail .id_event);
query3.equalTo("status", "confirmed")
query3.count({
success: function(number) {
detail["n_requests_confirmed"] = number;
// alert("confirmed")
res('Some value if required'); //These are needed or promise chain will hang
}
});
})]);
}));
你知道怎么做吗?
Best Answer-推荐答案 strong>
我使用 deferred 是为了让你可以运行一个 Promise 链,而不是将它们分开。如果调用发生错误,您还需要处理错误情况(两个 Promise 使用相同的错误函数)。它会正确地拒绝 promise ,以便您妥善处理。
var promises = [];
eventDetails.forEach(function(detail) {
var deferred = $q.defer();
new Parse.Query('Attendees')
.equalTo("event_id", detail.id_event)
.count().then(function(number) {
detail["n_requests_received"] = number;
alert("received");
return new Parse.Query('Attendees')
.equalTo("event_id", detail .id_event)
.equalTo("status", "confirmed")
.count();
}).then(function(confirmedCount) {
detail["n_requests_confirmed"] = confirmedCount;
deferred.resolve('I have resolved this promise');
}, function(err) {
console.error(JSON.stringify(err));
deferred.reject('Error: ' + JSON.stringify(err));
});
promises.push(deferred.promise());
});
$q.all(promises).then(function(res) {
// completed all promises w/ no errors
}, function(err) {
console.error(JSON.stringify(err));
});
关于javascript - 在 ios ionic 应用程序中使用 javascript promise ,我们在Stack Overflow上找到一个类似的问题:
https://stackoverflow.com/questions/38618329/
|