2
沿着一个承诺获得的资料,因此我有一个需要解决的问题,所以我可以在数据得到承诺的屈指可数。当然,我有方便的promise.all()
可供我使用。不幸的是,其中的一个承诺返回了另一组承诺。是否有可能获得这些承诺?从承诺与其他数据(promise.all)
例如:
var result = {};
let pName = getName(); // returns promise<string>
let pAge = getAge(); // returns promise<string>
let pSchools = getSchools; // returns promise<Array<promise<string>>>
return promise.all([pName, pAge, pSchool]).then(function(res) {
result["name"] = res[0];
result["age"] = res[1];
result["schools"] = [];
// This next section I don't think will work because it doesn't resolve "in time".
res[2].map(function(school) {
school().then(theSchool => result["schools"].push(theSchool))
});
return result;
}); // returns promise<{
// "name": "string",
// "age": "string",
// "schools": [string1, ..., stringN]
// }>
有没有人必须做这样的事情?你是如何工作的?
我给一个尝试 – Machtyn