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.

Share Improve this question edited Feb 2, 2016 at 23:08 CupawnTae 14.6k3 gold badges30 silver badges61 bronze badges asked Feb 2, 2016 at 22:39 Matt WelanderMatt Welander 8,55825 gold badges96 silver badges146 bronze badges 2
  • 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
Add a ment  | 

2 Answers 2

Reset to default 3

First, 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