Reading 04 - AvreyBrown-401-advanced-javascript/seattle-javascript-401d32 GitHub Wiki

Here is an example of the asynchronous version:

const fs = require('fs');

fs.unlink('/tmp/hello', (err) => { if (err) throw err; console.log('successfully deleted /tmp/hello'); });

Here is the synchronous version:

const fs = require('fs');

fs.unlinkSync('/tmp/hello'); console.log('successfully deleted /tmp/hello');

fs functions support passing and receiving paths as both strings and Buffers. The latter is intended to make it possible to work with filesystems that allow for non-UTF-8 filenames. For most typical uses, working with paths as Buffers will be unnecessary, as the string API converts to and from UTF-8 automatically.

two rules for defining an error-first callback:

The first argument of the callback is reserved for an error object. If an error occurred, it will be returned by the first err argument. The second argument of the callback is reserved for any successful response data. If no error occurred, err will be set to null and any successful data will be returned in the second argument.

In a synchronous programming model, things happen one at a time. When you call a function that performs a long-running action, it returns only when the action has finished and it can return the result. This stops your program for the time the action takes. An asynchronous model allows multiple things to happen at the same time. When you start an action, your program continues to run. When the action finishes, the program is informed and gets access to the result (for example, the data read from disk).