Q Promise Nodejs how to resolve in loop

2019-01-23 19:11发布

问题:

i have code written in nodejs make me confusying using Q Promises

theFunction()
.then(function(data) {
    var deferred = Q.defer()
    var result = [];
    for(i=0; i < data.length; i++) {

        secondFunc(data.item)
        .then(function(data2) {
            data.more = data2.item
        });
        result.push(data);

    }

    deferred.resolve(result);
    deferred.promise();

});

i want data in second function inside loop can push into result

so my previous data is like this

[
    {
        id: 1,
        item: 1,
        hero: 2
    },
    {
        id: 1,
        item: 1,
        hero: 2
    }
]

and so like this

[
    {
        id: 1,
        item: 1,
        hero: 2,
        more: {
            list: 1
        }
    },
    {
        id: 1,
        item: 1,
        hero: 2,
        more: {
            list: 4
        }

    }
]

I've tried several ways start by entering the command deferred.resolve (); statement in the loop and only showing 1 data have any solution ?

回答1:

Instead of a deferred.resolve() on an array which will resolve immediately, use Q.all which waits for an array of promises:

theFunction()
.then(function(data) {
    var result = [];
    for(var i=0; i < data.length; i++) (function(i){
        result.push(secondFunc(data[i].item)
        .then(function(data2) {
            data[i].more = data2.item;
            return data[i];
        }));
    })(i); // avoid the closure loop problem
    return Q.all(result)
});

Or even better:

theFunction()
.then(function(data) {
    return Q.all(data.map(function(item)
        return secondFunc(item)
        .then(function(data2) {
            item.more = data2.item;
            return item;
        });
    });
});


回答2:

I know this is a older post but I've the same problem and did not found any solution. Maybe someone here find a good solution very fast.

function CompareTeamspeakClients(forumUsers) {
  var promises = [];
  var tsClient = new TeamSpeakClient("127.0.0.1", 10011);

  tsClient.send("login", {
    client_login_name: "serveradmin",
    client_login_password: "M+h8YzUA"
  }, function(err, response){
    if (err) deferred.reject(err);
  });

  tsClient.send("use", {
    port: 9987
  }, function(err, response){
    if (err) deferred.reject(err);
  });

  forumUsers.forEach(function(user, index){
    var deferred = Q.defer();

    tsClient.send("clientdbfind", ["uid"], {
      pattern: user.tsid
    }, function(err, response){
      if (err) deferred.reject(err);
      if (response) {
        tsClient.send("clientdbinfo", {
          cldbid: response.cldbid
        }, function(err, response){
          if (err) deferred.reject(err);

          forumUsers[index]['tsdbid'] = response.client_database_id;
          forumUsers[index]['tsnickname'] = response.client_nickname;
          forumUsers[index]['tslastconnected'] = response.client_lastconnected;

          deferred.resolve(forumUsers);
        });
      }
    });

    promises.push(deferred.promise);
  });

  console.log(promises);

  return Q.all(promises);
}