async.queue永远不会耗尽?

时间:2016-07-31 11:38:09

标签: javascript node.js asynchronous queue

我正在尝试使用节点中的异步队列查询航班api,但它显示为我的队列永远不会“消耗”(只需运行drain方法)。 I am using this library

它运行每个请求都很好,但它只是停在那里,而排水功能中的内容永远不会执行。

let flights = [];
let q = async.queue(function (airline, callback) {
  const flight_search_url = 'http://someflightapi.com/search/' + airline.code + '?date=' + date + '&from=' + originAirportCode + '&to=' + destinationAirportCode;
  request(flight_search_url, function(error, response, body) {
    if (error) {
      return callback(error);
    }
    if (res.statusCode !== 200) {
      return callback(res.statusCode);
    }
    console.log(airline.code);
    flights.push(JSON.parse(body));
    callback();
  });
}, 10);

q.drain(function(error) {
  if (error) {
    res.json({
      error: "There was an error while calculating flights",
      destinationAirportCode: destinationAirportCode,
      originAirportCode: originAirportCode,
      possibleOrigins: possibleOrigins,
      possibleDestinations: possibleDestinations,
      flights: flights
    });
  } else {
    res.json(flights);
  }
});

q.push(airlines);

0 个答案:

没有答案