javascript - how to use promise.all inside then()-chain -
it may question title off, in case promise.all
not need solve issue.
i have then()
- chain in promise , have series of similar operations should handle differently, new promises.
return new promise(function(resolve, reject) { c.scraper.load().then(function () { .... var personurl = url + "/" + c.people[0]; return c.checkperson(personurl); }).then(function(){ var personurl = url + "/" + c.people[1]; return c.checkperson(personurl); }).then(function(){ var personurl = url + "/" + c.people[2]; return c.checkperson(personurl); ....
i think problem.
the first step combine these 3 one, , second step, if possible, make work unknown number of people in array c.people
.
first, since c.scraper.load()
returns promise, can return result of entire promise chain, , don't need wrap in new promise(...)
.
next, since c.people
seems array of paths want fetch, can map
array list of promises, use promise.all()
wait them resolve:
return c.scraper.load().then(function () { return promise.all(c.people.map(function (person) { var personurl = url + "/" + person; return c.checkperson(personurl); })); }).then(function (results) { // `results` array containing results of `checkperson()` calls });
does help?
Comments
Post a Comment