Await a list of fs.promises

Viewed 142

I want to verify that I'm awaiting fs.promises correctly.

I have entries in a map called res that I want to write to a file.

I know for sure that this would await them:

for(let [path, content] of res.entries()) {
   await fs.promises.writeFile(path, content)
}

But this is inefficient so I'm curious if this does it:

let promises = []
for(let [path, context] of res.entries()) {
   promises.push( fs.promises.writeFile(path, content) );
}
await Promise.all(promises)

The reason why I'm not sure if the second example works is because writeFile is simply an async function, so I'm not sure if the await is actually awaiting anything.

The other options would be to do something fancy to make a line like this possible Promise(fs.writeFile(...)) (which isn't possible as is) to await it.

1 Answers

Yes, the second snippet with Promise.all() will work as long as all the files you are writing to are separate files (no overlapping writes to the same file) and thus can all be done in parallel.

fs.promises.writeFile() returns a promise that resolves when the operation is done so your promises array will be an array of promises which is exactly what Promise.all() is expecting.


Note from the doc for fs.promises.writeFile():

Returns: <Promise>. Fulfills with undefined upon success.


Note, there is a limit to how much parallelism you can really get when writing to multiple files on the same disk because all the writes have to go through the same disk controller, over the same bus and (if a spinning disk) the head can only be on one track at a time.

In addition, nodejs uses a thread pool for disk operations which only uses a small number of threads for simultaneous requests. The OS and nodejs will handle all this for you (by queueing up writes until it's their turn) so there is no danger in doing it this way, but writing 20 files in parallel is probably not 20x faster than writing them in sequence, but it should still be faster in parallel. So if the code lends itself to running parallel, it is a desirable way to code it.

Related