The callback pattern in Node.js

Node.js is single-threaded, just like the web. To handle long running operations it also uses a callback pattern. The callback pattern in Node.js has a a few more details to it and can be described as having the following properties:

  • There is only one function to handle success and error responses
  • The callback is called only once
  • The function is the last parameter of the calling function
  • The callback consists of the parameter's errors and results, in that order, which is also called error-first

Let's now showcase what the calling code looks like, with a callback supplied as the function's last argument:

callAsync('1',2, (error, response) => {  if(error) {    console.error(error);  } else { console.log('response', ...

Get Architecting Angular Applications with Redux, RxJS, and NgRx 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.