Asynchronous File Writing
The asynchronous method of file writing puts a write request on the event queue and then returns control back to the calling code. The actual write does not take place until the event loop picks up the write request and executes it. You need to be careful when performing multiple asynchronous write requests on the same file because you cannot guarantee the execution order unless you wait for the first write callback before executing the next one. Typically the simplest way to do this is to nest writes inside the callback from the previous write. The code in Listing 6.3 illustrates this process.
To write to a file asynchronously, first open it using open()
and then after the callback from the open request has executed, ...
Get Node.js, MongoDB, and AngularJS Web Development now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.