Reputation: 999
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!
Upvotes: 17
Views: 24153
Reputation: 20236
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);
}
Upvotes: 1
Reputation: 664538
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!
});
}
Upvotes: 38
Reputation: 164139
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) });
});
}
Upvotes: 3