javascript - Getting data from a promise of a promise along with other data (promise.all) -


so have handful of promises need resolved can @ data. of course, have handy promise.all() available me. unfortunately, 1 of promises returns array of promises. possible @ array of promises?

for example:

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"] = [];     // next section don't think work because 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]      //  }> 

has had this? how did work it?

you need return results inner promise resolves schools :

return promise.all([pname, page, pschool]).then(function(res) {   return promise.all(res[2].map(school => school()))       .then(schools => {           result["name"] = res[0];           result["age"] = res[1];           result["schools"] = schools;           return result;       });    })); }); 

Comments

Popular posts from this blog

aws api gateway - SerializationException in posting new Records via Dynamodb Proxy Service in API -

asp.net - Problems sending emails from forum -