How to return a promise when writestream finishes?
Solution 1
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!
});
}
Solution 2
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) });
});
}
Lubor
I am currently a full-funded thesis-based Master (MCS) student, under the supervision of Prof.Stan Matwin (Canada Research Chair Tier 1), in Dalhousie University. I have been a Research Assistant in the Institute for Big Data Analytics since 2013. I am an open-minded and self-motivated person with strong team work spirit and great enthusiasm for Data Mining, Machine Learning and Big Data Analytics.
Updated on June 07, 2022Comments
-
Lubor almost 2 years
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!
-
Lubor over 7 yearsThanks! I think you are right. I don't need to pass a boolean. void shall be enough.
-
Guillermo Mansilla over 7 yearsOut of curiosity, if you're returning a boolean (or void) then do you really need a promise?
-
Bergi over 7 years@GuillemoMansilla you're returning a promise for a boolean. So you still know when the operation succeeds, or whether it doesn't.
-
Guillermo Mansilla over 7 yearsOh, I see. so the use case for this would be something like: writeToFile('foo').then(function(response) { console.log(response); // true }
-
Bergi over 7 years@GuillemoMansilla yes, exactly
-
Lubor over 7 yearsHi 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? -
Bergi over 7 years@Lubor Afaik, it's done when it emits the
finish
event. -
Lubor over 7 years@Bergi But in this code we use
file.end()
to emitfinish
event, right? -
Bergi over 7 years@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, in this answer I've just converted your original code to promise style without knowing many details about write streams. -
Lubor over 7 years@Bergi Yes, I think so. I tried to put break points to the line of
resolve()
after removingfile.end()
, and I can see the the program 'never' hit this line. I guess no finish event is emitted. -
umitu over 2 yearsThe 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. -
Bergi over 2 years@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.
-
Roko C. Buljan about 2 years
reject
is there for...