admin管理员组文章数量:1332881
It may be that my question title is off, in case the promise.all
is not what I need to solve this issue.
I have a then()
- chain in my promise and I have a series of similar operations that I really should handle differently, but I am new to 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 you get the problem.
The first step would be to bine these three into one, and the second step, if possible, would be to make it work with an unknown number of people in the array c.people
.
It may be that my question title is off, in case the promise.all
is not what I need to solve this issue.
I have a then()
- chain in my promise and I have a series of similar operations that I really should handle differently, but I am new to 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 you get the problem.
The first step would be to bine these three into one, and the second step, if possible, would be to make it work with an unknown number of people in the array c.people
.
-
1
Avoid the
Promise
constructor antipattern! – Bergi Commented Feb 2, 2016 at 23:56 -
Don't forget
.catch(e => console.error(e));
at the end so you can see if you make an error! – jib Commented Feb 3, 2016 at 7:36
2 Answers
Reset to default 3First, since c.scraper.load()
itself returns a promise, you can return the result of the entire promise chain, and you don't need to wrap it in new Promise(...)
.
Next, since c.people
seems to be an array of paths you want to fetch, you can map
the array to a list of promises, then use Promise.all()
to wait for them all to 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` is an array containing the results of the `checkPerson()` calls
});
Does that help?
Assuming c.checkPerson
returns a Promise, you can do something like this:
var promiseArray = c.people.map(function(person) {
var personUrl = url + "/" + person;
return c.checkPerson(personUrl)
})
Promise.all(promiseArray).then(function() {
// plete
})
本文标签: javascripthow to use promiseall inside then()chainStack Overflow
版权声明:本文标题:javascript - how to use promise.all inside then()-chain - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742310898a2450849.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论