如何在async .series中执行async.each

时间:2016-06-22 17:44:26

标签: node.js asynchronous callback

在我的函数中,我必须在异步foreach中调用异步系列来计算最终结果并创建可能的json.is

function downloadFile($fileID)
        {
            $fileID = mysqli_escape_string($this->conn, $fileID);

            $query = "SELECT * FROM file_store WHERE id='$fileID'";
            $result = $this->conn->query($query);
            if ($result)
            {
                $data = $result->fetch_array();
                header("Content-length: " . $data["Size"]);
                header("Content-type: " . $data["ContentType"]);
                header("Content-Disposition: attachment; filename=" . $data["FileName"]);
                echo $data["FileContent"];
            }
            else
            {
                echo mysqli_error($this->conn);
            }
        }

1 个答案:

答案 0 :(得分:0)

您应该能够将尽可能多的同步函数嵌套到彼此中,但最好使用命名约定,以便您可以轻松跟踪哪些回调传递到哪里并避免因提升而发生冲突。所以基本上这应该像你期望的那样工作:



async.series([
  function first(seriesCallback) {
    seriesCallback();
  },
  // other functions in series
  // ...
  function (seriesCallback) {
    var someArray = [];
    async.each(someArray, function (value, eachCallback) {
      // process the value
      // return an error if there is one
      eachCallback(err);
    }, function(err) {
      // add any additional processing you might need
      // pass the control to the parent async method and handle the errors
      // in a more central place if there is an error here it will
      // be processed in onSeriesDone as well as all other errors
      seriesCallback(err);
    });
  }], function onSeriesDone(err) {
    next(err);
  });