Node.js承诺和for循环

我设法得到这个,但我再次卡住了。

我的for循环不按我想要的方式工作。 如果有三个angular色,只有最后一个被添加(三次)到我的主轴MySQL表。 (因为它是asynchronous的)

我怎样才能让它“以平行的方式工作,但在添加他/她之后添加一个人的angular色”? 还有其他types的东西我想要做下载图像,所以我正在寻找一种方法来实现“做到这一点,得到这个信息,然后做”。

我正在使用蓝鸟。

如果太明显,我很抱歉,我不明白该怎么做。

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); }); }); }); 

您将需要使用闭包来保存asynchronous函数的i上下文。 不过,我相信有一个更有希望的方式来做到这一点。

例:

 (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. .... }); })();