Asychronous

An asynchronous callback is executed after the execution of the higher order function.
The higher order function will complete without having to wait for the callback function to finish executing.



In this example the named function runLater is an asynchronous callback function.

console.log('setTimeout() has started'); 
setTimeout( function runLater() { console.log("runLater() is called'); } , 2000);
console.log('setTimeout() has finished');


function asyncMethod(message, callback_function) { 
  setTimeout( function() {
    console.log(message);
    callback_function();
   }, 400)
}

asyncMethod('Connect to Database', function() {
}


Nested Callbacks


asyncMethod('Connect to Database', function() { 
  asyncMethod('Verify User Details', function() {
    asyncMethod('Check Permissions', function() {
      asyncMethod('Update User', function() {} )
    })
  })
})

Best Practices

Arguments should be (err, results)
Always 'return' when you want to exit




© 2023 Better Solutions Limited. All Rights Reserved. © 2023 Better Solutions Limited TopPrevNext