15

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!

Nitzan Tomer
  • 155,636
  • 47
  • 315
  • 299
Lubor
  • 989
  • 3
  • 10
  • 33
  • Duplicate of [How do I convert an existing callback API to promises?](https://stackoverflow.com/q/22519784/1048572) – Bergi Dec 27 '22 at 23:07

2 Answers2

37

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!
  });
}
Bergi
  • 630,263
  • 148
  • 957
  • 1,375
  • Thanks! I think you are right. I don't need to pass a boolean. void shall be enough. – Lubor Oct 05 '16 at 18:13
  • Out of curiosity, if you're returning a boolean (or void) then do you really need a promise? – Guillermo Mansilla Oct 05 '16 at 18:39
  • 1
    @GuillemoMansilla you're returning a *promise for* a boolean. So you still know when the operation succeeds, or whether it doesn't. – Bergi Oct 05 '16 at 19:34
  • Oh, I see. so the use case for this would be something like: writeToFile('foo').then(function(response) { console.log(response); // true } – Guillermo Mansilla Oct 05 '16 at 19:40
  • @GuillemoMansilla yes, exactly – Bergi Oct 05 '16 at 19:43
  • 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? – Lubor Oct 18 '16 at 19:53
  • @Lubor Afaik, it's done when it emits the `finish` event. – Bergi Oct 18 '16 at 19:58
  • @Bergi But in this code we use `file.end()` to emit `finish` event, right? – Lubor Oct 18 '16 at 20:04
  • @Lubor Do we? I don't think so, `.end()` has to be asynchronous as well to work. But maybe you should [ask a new question](http://stackoverflow.com/questions/ask), in this answer I've just converted your original code to promise style without knowing many details about write streams. – Bergi Oct 18 '16 at 20:06
  • @Bergi 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. – Lubor Oct 18 '16 at 20:09
  • 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. – umitu Dec 19 '21 at 22:36
  • @umitu 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. – Bergi Dec 19 '21 at 22:40
  • 1
    This answer is being discussed on meta: https://meta.stackoverflow.com/questions/422349/is-it-abuse-to-answer-then-dupe-hammer-a-question – Kaiido Dec 27 '22 at 06:58
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) });
    });
}
Nitzan Tomer
  • 155,636
  • 47
  • 315
  • 299