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', response);
// do something with the response
}
})

This piece of code fulfills all the properties laid out by the pattern, namely that the last parameter in the function call is the callback function. Furthermore, the callback function has the error as the first parameter and the response as the second parameter. In addition, the body of the callback function checks whether there is an error first and then, in the absence of an error, deals with the response we get back. 

For reference, let's also look at how callAsync() is implemented:

function callAsync(param, param2, fn) {
setTimeout(() => {
if(param > param2) {
fn(null, 'success');
} else {
fn('error', null);
}
}

The preceding implementation is just a mockup but it does show off two important aspects. One aspect is the time factor the setTimeout() function represents and the fact that the function takes time to complete.

The other aspect is our third parameter, fn() which is invoked differently. We call fn(null, 'success') when everything is alright and fn('error', null) when an error has occurred. The way we invoke fn() is how we communicate success and failure.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.15.175.101