Chapter 7. Optimizing Code Performance and Control Flow Management Using the Async Library
Writing or using functions or methods in your code that are executed one after the other gets you a long way in your applications.
Sometimes, those functions are simple synchronous steps:
console.log('Starting calculation...'); var result = 5 + 4; console.log('The result is', result);
Often, callbacks are used when operations are executed in the background and jump back into our code's control flow asynchronously at a later point in time:
console.log('Starting calculation...'); startExpensiveCalculation(5, 4, function(err, result) { if (!err) { console.log('The result is', result); } });
If those asynchronous background operations bring forth a more complex callback behaviour, they might be implemented as an event emitter:
console.log('Starting calculation...'); calculation = Calculator.start(5, 4); calculation.on('error', function(err) { console.log('An error occured:', err); }); calculation...