Reputation: 2564
I am writing modifications to a file based upon user input using fs.writeFile(...)
. I recently encountered a bug where two write requests came in effectively causing the file to be truncated.
Each write operation must complete in the order it was received. However I don't want the user to have to wait for the write to complete before they can move on to their next task.
What is the canonical way or popular module to queue the async writes such that the user is not prevented from doing other work while the writes happen, and to guarantee collision does not occur.
Additionally I'd like reads to happen to the same file if possible.
My initial thoughts would be write a temp file and then copy, but that seems not as efficient.
Upvotes: 2
Views: 3248
Reputation: 1658
Use async.queue from the async module to create a task runner with a concurrency of 1 (only one operation can run at a time). When a new edit occurs add it to the queue. Your logic would be as follows:
Upvotes: 5