简体   繁体   中英

Node.js promises and for loop

I manage to get this far, but I'm stuck again.

My for loops doesn't work the way I want. If there are three roles, only the last one is being added (three times) to my pivot MySQL table. (since it's asynchronous)

How can I make it work in a way "run parallel, but add a person's roles after you add him/her"? There are also other type of things I want to do like downloading images, so I'm looking for a way to achieve "do this, get this info, then do that".

I'm using bluebird.

I'm sorry if it's too obvious, I couldn't understand how to do it.

var entities = require("entities");
var request = require('request');
var cheerio = require('cheerio');

var person = require('./models').person;
var personRole = require('./models').personRole;
var personPersonRole = require('./models').personPersonRole;

// create promisified version of request()
function requestPromise(options) {
  return new Promise(function (resolve, reject) {
    request(options, function (err, resp, body) {
      if (err) return reject(err);
      resolve(body);
    });
  });
}

app.get('/fetch2', function (req, res) {
  var promises = [];
  var headers = {
    'User-Agent': req.headers['user-agent'],
    'Content-Type': 'application/json; charset=utf-8'
  };
  for (var i = 0; i < 10; i++) {
    promises.push(requestPromise({url: "http://www.example.com/person/"+ i +"/personname.html", headers: headers}));
  }
  Promise.all(promises).then(function (data) {
    // iterate through all the data here
    data.forEach(function (item, i) {
      var $ = cheerio.load(item);
      var name = $("#container span[itemprop='name']").text();
      if (!name) {
        console.log("null name returned, do nothing");
      } else {
        // name exists
        person.where('id', i).fetch({require: true}).then(function (p) {
          console.log("exists");
        }).catch(function () {
          console.log(i + " does not exist");
          new person({id: i, name: name}).save(null, {method: 'insert'}).then(function (returnval) {

            var numberOfRoles = $('span[itemprop="jobtitle"]').length;
            for (var b = 0; b < numberOfRoles; b++) {
              var personType = $('span[itemprop="jobtitle"]').eq(b).text();
              var personType = entities.decodeHTML(personType);
              personRole.where('person_role', personType).fetch().then(function (pResult) {
                new personPersonRole({
                  person_id: i,
                  personrole_id: pResult.id
                }).save();
              }).catch(function () {
                console.log("this role doesn't exist in the database, now it will be added. then this role's id and" +
                  "person id will be added to the pivot table");
                new personRole({
                  person_type: personType
                }).save().then(function (data5) {
                  new personPersonRole({
                    person_id: i,
                    personrole_id: data5.id
                  }).save();
                });
              });
            }
          });
        });
      }
    }, function (err) {
      // error occurred here
      console.log(err);
    });
  });
});

You will need to use a closure in order to save the i context for asynchronous functions. I believe there is a more Promises-styled way to do this, however.

Example:

(function() {
    var personId = i;
    person.where('id', personId).fetch({require: true}).then(function (p) {
        console.log(personId); // This will give the 'saved' value, instead of whatever i is when the callback finishes.
        ....
    });
})();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM