nathasm
nathasm

Reputation: 2564

Multiple async writes to the same file in node

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

Answers (1)

Evan Shortiss
Evan Shortiss

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:

  1. Add task to queue.
  2. When task runs read the existing file.
  3. Perform your write logic.
  4. Call the async queue callback so the next queued write can occur.

Upvotes: 5

Related Questions