如何等待流完成管道? (的NodeJS)

时间:2016-06-15 13:38:36

标签: node.js asynchronous promise pipe pdftotext

我有一个for循环的promises数组,所以我使用Promise.all来完成它们然后调用它们。

let promises = [];
promises.push(promise1);
promises.push(promise2);
promises.push(promise3);

Promise.all(promises).then((responses) => {
  for (let i = 0; i < promises.length; i++) {
    if (promise.property === something) {
      //do something
    } else {
      let file = fs.createWriteStream('./hello.pdf');
      let stream = responses[i].pipe(file);
      /*
         I WANT THE PIPING AND THE FOLLOWING CODE 
         TO RUN BEFORE NEXT ITERATION OF FOR LOOP
      */
      stream.on('finish', () => {
        //extract the text out of the pdf
        extract(filePath, {splitPages: false}, (err, text) => {
        if (err) {
          console.log(err);
        } else {
          arrayOfDocuments[i].text_contents = text;
        }
      });
    });    
  }
}

promise1,promise2和promise3是一些http请求,如果其中一个是application / pdf,那么我将其写入流并从中解析文本。但是这段代码在解析pdf中的测试之前会运行下一次迭代。有没有办法让代码等到流到流和提取的管道完成后再继续下一次迭代?

4 个答案:

答案 0 :(得分:12)

以下内容也适用。我经常使用这种模式:

let promises = [];
promises.push(promise1);
promises.push(promise2);
promises.push(promise3);

function doNext(){
  if(!promises.length) return;
  promises.shift().then((resolved) =>{
    if(resolved.property === something){
      ...
      doNext();
    }else{
      let file = fs.createWriteStream('./hello.pdf');
      let stream = resolved.pipe(file);
      stream.on('finish', () =>{
        ...
        doNext();
      });
    }

  })
}
doNext();

或将处理程序分解为控制器和Promisified处理程序:

function streamOrNot(obj){
  return new Promise(resolve, reject){
    if(obj.property === something){
      resolve();
      return;
    }
    let file = fs.createWriteStream...;
    stream.on('finish', () =>{
      ...
      resolve();
    });
  }
}

function doNext(){
  if(!promises.length) return;
  return promises.shift().then(streamOrNot).then(doNext);
}

doNext()

答案 1 :(得分:6)

没有异步/等待,这很讨厌。使用async / await,只需执行以下操作:

Promise.all(promises).then(async (responses) => {
  for (...) {
    await new Promise(fulfill => stream.on("finish", fulfill));
    //extract the text out of the PDF
  }
})

答案 2 :(得分:0)

您可以在自调用函数中编写else部分。这样流的处理将并行发生

(function(i) {
    let file = fs.createWriteStream('./hello.pdf');
    let stream = responses[i].pipe(file);
  /*
     I WANT THE PIPING AND THE FOLLOWING CODE 
     TO RUN BEFORE NEXT ITERATION OF FOR LOOP
  */
    stream.on('finish', () => {
      //extract the text out of the pdf
      extract(filePath, {splitPages: false}, (err, text) => {
      if (err) {
        console.log(err);
      } 
      else {
        arrayOfDocuments[i].text_contents = text;
      }
    });
  });    
})(i) 

否则,您可以将流媒体部分作为原始/个人承诺本身的一部分来处理。

截至目前,您正在创建承诺并将其添加到数组中,而不是将promise.then添加到数组中(这也是一个承诺)。然后在处理程序内部进行流式处理。

答案 3 :(得分:0)

使用 awaitstream.pipeline() 而不是 stream.pipe()

import * as StreamPromises from "stream/promises";

...
await StreamPromises.pipeline(sourceStream, destinationStream);