我遍历数组,但每次迭代,我打电话异步方法。
下面是代码
var getTaskResponse = function(displayedWidgetId) {
var promises = [];
// var promise = new Parse.Promise();
jQuery.ajax({
type: "POST",
url: 'https://api.parse.com/1/functions/getWidgetResponse',
data: {
"groupWidgetId": displayedWidgetId
},
beforeSend: function(request) {
request.setRequestHeader("X-Parse-Application-Id", parse_ids.app_id);
request.setRequestHeader("X-Parse-REST-API-Key", parse_ids.rest_api);
}
})
.then(function(response){
var promise = new Parse.Promise();
var taskIssuedTo = {},
taskObj = {},
finalObj = {},
subTasks = [],
taskResponse = response.result;
taskResponse.forEach(function(widgetResponseObj,index) {
var issuedTo = widgetResponseObj.answeredBy.objectId,
taskResponseObj = JSON.parse(widgetResponseObj.widgetResponse),
taskSelected = taskResponseObj.taskSelected,
date = taskResponseObj.date;
(function(userId){
getUsersName(userId).then(function(userName){
if (typeof taskIssuedTo[taskSelected] === "undefined") {
taskIssuedTo[taskSelected] = [];
subTasks = [];
}
subTasks.push({
"user" : userName.result.name,
"date" : date
});
taskIssuedTo[taskSelected] = subTasks;
taskObj[taskSelected] = taskIssuedTo[taskSelected];
});
}(issuedTo));
});
promise.resolve(taskObj);
return promise;
})
.then(function(obj){
console.log("After Promise Return :: "+JSON.stringify(obj));
});
}
另外,在上述for-each loop
getUsersName是得到调用的方法。 当我打印解决的承诺,这是我得到了什么
承诺回报后:: {"_resolved":true,"_rejected":false,"_resolvedCallbacks":[],"_rejectedCallbacks":[],"_result":{"0":{}}}
我应该怎么解决的承诺,让我得到所需的输出?