javascriptnode.jspipelinenode-streams

Better way to return a promise that resolves to a stream in Node JS


I'm stuck somehow, I have something like this in my code:

async function download() {

  const source = createReadStreamSomeHow();

  await pipeline(source, fs.createWriteStream("file.ext"));

  return source;
}

I need to return the readable stream (source) and store it in an array somewhere and at the same time pipe the data from the read stream to "file.ext". But when I call:

let task = await download();
taskArray.add(task);

the pipeline() code pauses execution of the function, hence source is only returned when the data has been completely piped to "file.ext".

Although I understand why the code behaves this way, I can't seem to find a way to return the source stream from the download() function and still pipe the data to "file.ext" at the same time. Is there a better way to achieve this and make it work? Thank for the help in advance.


Solution

  • you can simply drop the async/await

    function download() {
    
      const source = createReadStreamSomeHow();
    
      pipeline(source, fs.createWriteStream("file.ext"));
    
      return source;
    }
    

    The Promise is still executed, but you have no info when it is done/if it throws.

    Or you return the source together with the promise

    function download() {
      const source = createReadStreamSomeHow();
    
      const promise = pipeline(source, fs.createWriteStream("file.ext"));
    
      return { source, promise }
    }
    
    // you can enqueue the source ... 
    let task = download();
    taskArray.add(task.source);
    
    // ... and handle the promise however you like.
    await task.promise;