How to return a promise when writestream finishes?
Asked Answered
M

3

17

I have such a function, which create a write stream and then write the string array into the file. I want to make it return a Promise once the writing is finished. But I don't know how I can make this work.

function writeToFile(filePath: string, arr: string[]): Promise<boolean> {
   const file = fs.createWriteStream(filePath);
   arr.forEach(function(row) {
     file.write(row + "\n");
   });
   file.end();
   file.on("finish", ()=>{ /*do something to return a promise but I don't know how*/});
}

Thank you for any comment!

Metropolitan answered 5/10, 2016 at 18:2 Comment(1)
Duplicate of How do I convert an existing callback API to promises?Epode
E
38

You'll want to use the Promise constructor:

function writeToFile(filePath: string, arr: string[]): Promise<boolean> {
  return new Promise((resolve, reject) => {
    const file = fs.createWriteStream(filePath);
    for (const row of arr) {
      file.write(row + "\n");
    }
    file.end();
    file.on("finish", () => { resolve(true); }); // not sure why you want to pass a boolean
    file.on("error", reject); // don't forget this!
  });
}
Epode answered 5/10, 2016 at 18:11 Comment(13)
Thanks! I think you are right. I don't need to pass a boolean. void shall be enough.Metropolitan
Out of curiosity, if you're returning a boolean (or void) then do you really need a promise?Stallworth
@GuillemoMansilla you're returning a promise for a boolean. So you still know when the operation succeeds, or whether it doesn't.Epode
Oh, I see. so the use case for this would be something like: writeToFile('foo').then(function(response) { console.log(response); // true }Stallworth
@GuillemoMansilla yes, exactlyEpode
Hi Bergi, I just had a follow up question. Isn't writableStream.write() asynchronous? If yes, how can we know when all the file.write(row) statements have been executed?Metropolitan
@Metropolitan Afaik, it's done when it emits the finish event.Epode
@Epode But in this code we use file.end() to emit finish event, right?Metropolitan
@Metropolitan Do we? I don't think so, .end() has to be asynchronous as well to work. But maybe you should ask a new question, in this answer I've just converted your original code to promise style without knowing many details about write streams.Epode
@Epode Yes, I think so. I tried to put break points to the line of resolve() after removing file.end(), and I can see the the program 'never' hit this line. I guess no finish event is emitted.Metropolitan
The documentation of Writable.write() says that: If false is returned, further attempts to write data to the stream should stop until the 'drain' event is emitted. but your (and OP's) code doesn't handle it.Harbin
@Harbin IIRC it's still "safe" but inefficient - if you keep writing, the stream buffer will just keep growing (potentially until running out of memory). But it's not my code, I just copied that bit over from the OP's question. Indeed, I would not recommend writing it like that.Epode
This answer is being discussed on meta: meta.#422849Videogenic
P
3

You need to return the Promise before the operation was done.
Something like:

function writeToFile(filePath: string, arr: string[]): Promise<boolean> {
    return new Promise((resolve, reject) => {
        const file = fs.createWriteStream(filePath);
        arr.forEach(function(row) {
            file.write(row + "\n");
        });
        file.end();
        file.on("finish", () => { resolve(true) });
    });
}
Paranymph answered 5/10, 2016 at 18:8 Comment(1)
reject is there for...Frankenstein
C
1

With more recent Node.js versions, you can use the utility function finished from the stream package, like so:

import { finished } from 'node:stream/promises';
import { createWriteStream } from 'node:fs';

function writeToFile(filePath: string, arr: string[]): Promise<boolean> {
  const file = createWriteStream(filePath);
  arr.forEach((row) => file.write(row + "\n"));
  file.end();
  return finished(file);
}
Calctufa answered 9/8 at 11:23 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.