Both examples you provided simply use callbacks to deliver a result or error - the most basic form of asynchronous processing where you provide a callback function to continue processing later once either result or error become available, so e.g. fs.readfile() can simply return - non-blocking - without reading any of the file yet (The Node.js Event Loop, Timers, and process.nextTick()).
For an example of event driven programming see How to use fs.createReadStream? The capabilities revolve around
EventEmitter
.Both examples you provided simply use callbacks to deliver a result or error - the most basic form of asynchronous processing where you provide a callback function to continue processing later once either result or error become available, so e.g.
fs.readfile()
can simply return - non-blocking - without reading any of the file yet (The Node.js Event Loop, Timers, andprocess.nextTick()
).fs.readFileSync()
is the synchronous, blocking versionfsPromises.readFile()
is the promises based version that can be used withasync
/await
.