node.js:如何在forEach循环中使用异步调用实现路由方法?

时间:2015-09-15 16:07:54

标签: javascript node.js asynchronous

我必须实现这样的逻辑:

var express = require('express'),
    request = require('request');

var array = [
  { url: 'http://www.example1.com' },
  { url: 'http://www.example2.com' },
];

express.Router().get('/job', function (req, res) {
  results = [];
  array.forEach(function (item) {
    request(item.url, function (error, response, body) {
      if (!error && response.statusCode == 200) {
        results.push(body);
      }
  });
  res.json(results); // <== wrong, results array is empty, here...
});

如何调用res.json(结果)确保每个循环结束?

更新是建议答案的副本!

当所有请求都完成时我需要res.json(),而当forEach循环结束时我需要 ...: - (

3 个答案:

答案 0 :(得分:3)

实现这一目标的简单方法:

var express = require('express'),
    request = require('request');

var array = [
  { url: 'http://www.example1.com' },
  { url: 'http://www.example2.com' },
];

//store number of completed calls
var completed = 0;

express.Router().get('/job', function (req, res) {
  results = [];
  array.forEach(function (item) {
    request(item.url, function (error, response, body) {
      if (!error && response.statusCode == 200) {
        results.push(body);

        completed = completed + 1; //iterate counter
        onComplete(); //attempt completion
      }
  });

  function onComplete(){
    if(completed != array.length) {
      return;
    }
    res.json(results); 
    // ... rest of code that relies on results
  };

});

答案 1 :(得分:2)

您可以使用asnyc series来实现此目的。像这样:

var express = require('express'),
    request = require('request');
    async = require('async);

...

async.series([
    function(callback) {
        request('http://www.example1.com', function (error, response, body) {
            if (!error && response.statusCode == 200) {
                callback(null, body);
            }
        });
    },
    function(callback) {
        request('http://www.example2.com', function (error, response, body) {
            if (!error && response.statusCode == 200) {
                callback(null, body);
            }
        });
    }
], function(err, res) {
    res.json(res);
});

答案 2 :(得分:1)

你应该使用承诺! (此示例适用于节点的最后一个版本,因为它支持本机承诺)

var arrayPromises = [];

array.forEach(function (item) {

   arrayPromises.push(new Promise(function(resolve, reject) {
       request(item.url, function (error, response, body) {
          if (!error && response.statusCode == 200) {
             resolve(body);
          }else{
            resolve();
          }
       });
   }));
});

Promise.all(arrayPromises).then(function(results){
  res.json(results);
});