【问题标题】:awaiting completion of a highland stream等待高地溪流的完成
【发布时间】:2019-09-05 23:46:09
【问题描述】:

我正在编写一个小脚本来流式下载和处理来自 url 的多个按顺序命名的文件。我正在使用 highlandjs 并让它一一完美地工作。我正在尝试重构为一个循环,在该循环中我等待一个高地流的完成,然后再开始另一个:

// my attempt to abstract the highland into a promise I can await  
const processFile = async (url, path) => {
      const writeStream = fs.createWriteStream(path);

      return hl(request(url))
        .split()
         // various working transforms
        .map(splitDelim)
        .filter(filterSchedAOnly)
        .map(appendIdentity)
        .filter(filterOnlyIdentified)
        .map(convertToCSVsafeString)
        // end result should write to a file
        .pipe(writeStream)
        .toPromise();

   // also tried this
  // return new Promise((resolve, reject) => {
  //   writeStream.on("end", () => resolve());
  //   writeStream.on("error", () => reject());
  // });
};

(async () => {
  let i = 1;
  // infinite loop
  while (true) {
    const url = `http://.../${i}.txt`;
    const filePath = `${i}.processed.csv`;
    try {
      // does not work!
      await processFile(url, filePath);
    } catch (err) {
      console.log(err);
    }
    i++;
  }
})();

我应该如何包装我的 processFile 函数,以便在继续下一次迭代之前等待它完成?

【问题讨论】:

    标签: node.js highland.js


    【解决方案1】:

    这似乎有效:

    function streamToPromise(stream) {
      return new Promise(function(resolve, reject) {
        // resolve with location of saved file
        stream.on("finish", () => resolve());
        // stream.on("end", () => resolve());
        stream.on("error", () => reject());
      });
    }
    
    const processFile = async (url, path, i) => {
    
      const stream = hl(request(url))
        .split()
        // ......
        .pipe(fs.createWriteStream(path));
    
      return streamToPromise(stream);
    
    };
    

    【讨论】:

    • 我认为你可以删除你的 streamToPromise 函数并使 processFile 的返回语句为:return _(stream).toPromise(Promise);
    猜你喜欢
    • 1970-01-01
    • 2023-04-07
    • 2013-03-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-12-16
    • 1970-01-01
    • 2017-07-11
    相关资源
    最近更新 更多