SlideShare a Scribd company logo
Controlling the Callback Flow
Jason
NodeJs
Controlling the Callback Flow
● Understanding the boomerang effect
● Using async to control callback flow
Executing in Series
Executing in Parallel
Cascading
Queuing
Iterating
Mapping
Reducing
A demonstration of the boomerang effect.
var fs = require('fs');
function append_some_a_to_b(callback) {
fs.open(__dirname + '/a.txt', 'r', function(err, aFd) {
var buffer = new Buffer(10);
fs.read(aFd, buffer, 0, buffer.length, 0, function(err) {
fs.close(aFd, function(err) {
fs.open(__dirname + '/b.txt', 'a', function(err, bFd) {
fs.fstat(bFd, function(err, bStats) {
fs.write(bFd, buffer, 0, buffer.length, bStats.size,
function(err) {
fs.close(bFd, callback);
});
});
});
});
});
});
}
Using async to control callback flow
● To install async you include it in your package.json manifest or simply install
it
on your application root directory like this:
> npm install async
● The async module comes with a series of helper functions that allow you to
perform asynchronous iteration and flow control.
Executing in Series
series(tasks, [callback])
● Run the functions in the tasks array in series, each one running once the previous function has
completed.
● If any functions in the series pass an error to its callback, no more functions are run, and callback
is immediately called with the value of the error
● tasks - An array or object containing functions to run, each function is passed a callback(err,
result) it must call on completion with an error err (which can be null) and an optional result value
● callback - Callback receives an array of results when tasks have completed.
Executing in Series
var async = require('async');
async.series([
function(callback){
callback(null, 'one');
},
function(callback){
callback(null, 'two');
}
],
function(err, results){
if (err) { throw err; }
console.log('results: %j', results); // results is now equal to ['one', 'two']
});
Executing in Series
var async = require('async');
async.series({
one: function(callback){
setTimeout(function(){ callback(null, 1); }, 200);
},
two: function(callback){
setTimeout(function(){ callback(null, 2); }, 100);
}
},
function(err, results) {
if (err) { throw err; }
console.log('results: %j', results); // results is now equal to: {one: 1, two: 2}
});
Executing in Parallel
parallel(tasks, [callback])
● Run the tasks array of functions in parallel, without waiting until the previous function has
completed
● If any functions in the series pass an error to its callback, no more functions are run, and callback
is immediately called with the value of the error
● tasks - An array or object containing functions to run. Each function is passed a callback(err,
result) which it must call on completion with an error err (which can be null) and an optional result
value.
● callback(err, results) - An optional callback to run once all the functions have completed. This
function gets a results array (or object) containing all the result arguments passed to the task
Executing in Parallel
var async = require('async');
async.parallel([
function(callback){
setTimeout(function(){ callback(null, 'one'); }, 200);
},
function(callback){
setTimeout(function(){ callback(null, 'two'); }, 100);
}
],
function(err, results) {
if (err) { throw err; }
console.log('results: %j', results); // results is now equal to: {one: 1, two: 2}
});
Executing in Parallel
var async = require('async');
async.parallel({
one: function(callback){
setTimeout(function(){ callback(null, 1); }, 200);
},
two: function(callback){
setTimeout(function(){ callback(null, 2); }, 100);
}
},
function(err, results) {
if (err) { throw err; }
console.log('results: %j', results); // results is now equal to: {one: 1, two: 2}
});
Cascading
waterfall(tasks, [callback])
● Runs the tasks array of functions in series, each passing their results to the next in the array.
● If any of the tasks pass an error to their own callback, the next function is not executed, and the
main callback is immediately called with the error.
● tasks - An array of functions to run, each function is passed a callback(err, result1, result2, ...) it
must call on completion. The first argument is an error (which can be null) and any further
arguments will be passed as arguments in order to the next task.
● callback(err, [results]) - An optional callback to run once all the functions have completed. This will
be passed the results of the last task's callback.
Cascading
var async = require('async');
async.waterfall([
function(callback){
callback(null, 'one', 'two');
},
function(arg1, arg2, callback){
callback(null, 'three');
},
function(arg1, callback){
callback(null, 'done');
}
], function (err, result) {
console.log('result : ', result);
});
Queuing
queue(worker, concurrency)
● If you need to perform some repetitive asynchronous jobs and you want to control the
concurrency
● worker(task, callback) - An asynchronous function for processing a queued task, which must call
its callback(err) argument when finished, with an optional error as an argument.
● concurrency - An integer for determining how many worker functions should be run in parallel.
Queuing
var async = require('async');
var q = async.queue(function (task, callback) {
console.log('hello ' + task.name);
callback();
}, 2);
q.drain = function() { console.log('all items have been processed'); }
q.push( {name: 'foo'}, function (err) { console.log('finished processing foo'); });
q.push( {name: 'bar'}, function (err) { console.log('finished processing bar'); });
q.push( [{name: 'baz'},{name: 'bay'},{name: 'bax'}] , function (err) { console.log('finished processing item'); });
q.unshift( {name: 'bar'}, function (err) { console.log('finished processing bar'); });
Iterating
forEach(arr, iterator, callback)
● If you use this async.forEach function, you are invoking an iterator for each element in parallel
● iterator(item, callback) - A function to apply to each item in arr.
● callback(results) - A callback which is called when all iterator functions have finished, or an
error occurs.
Iterating
var async = require('async');
var results = {};
function iterator(value, callback) {
results[value] = Math.pow(value, 2);
callback();
}
async.forEach([1, 2, 3, 4], iterator, function(err){
if (err) { throw err; }
console.log('results: %j', results);
});
Output :
results: {“1”: 1, “2” : 4, “3” : 9, “4” : 16}
Mapping
map(arr, iterator, callback)
● Produces a new array of values by mapping each value in arr through the iterator function
● iterator(item, callback) - A function to apply to each item in arr. The iterator is passed a
callback(err, transformed) which must be called once it has completed with an error (which can be
null) and a transformed item.
● callback(err, results) - A callback which is called when all iterator functions have finished, or an
error occurs. Results is an array of the transformed items from the arr.
Mapping
var async = require('async');
function iterator(value, callback) {
callback(null, Math.pow(value, 2));
}
async.map([1, 2, 3, 4], iterator, function(err, results) {
if (err) {
throw err;
}
console.log('results: %j', results);
});
Output : results : [1,4,9,16]
Reducing
reduce(arr, memo, iterator, callback)
● Reduces array into a single value using an async iterator to return each successive step.
● memo - The initial state of the reduction.
● iterator(memo, item, callback) - A function applied to each item in the array to produce the next
step in the reduction. The iterator is passed a callback(err, reduction) which accepts an optional
error as its first argument, and the state of the reduction as the second.
● callback(err, result) - A callback which is called after all the iterator functions have finished. Result
is the reduced value.
Reducing
var async = require('async');
async.reduce([1,2,3], 0, function(memo, item, callback){
console.log(‘memo:‘ + memo, ‘item:’ + item);
callback(null, memo + item);
}, function(err, result){
console.log('Result : %j', result);
});
Output :
memo : 0 item : 1
memo : 1 item : 2
memo : 3 item : 3
Result : 6
Filtering
filter(arr, iterator, callback)
● Returns a new array of all the values in array which pass an async truth test.
● The callback for each iterator call only accepts a single argument of true or false
● It does not accept an error argument first!
Filtering
var async = require('async');
function filter(item, callback) {
callback(Math.pow(item, 2) > 10);
}
async.filter([1, 2, 3, 4, 5], filter, function(results)
{
console.log('Square value is greater than 10: %j', results);
});
Output : Square value is greater than 10: [4,5]
Filtering
var async = require('async');
var fs = require(‘fs’);
async.filter(['file1','file2','file3'], fs.exists, function(results){
// results now equals an array of the existing files
});
Detecting
detect(arr, iterator, callback)
● Returns the first value in array that passes an async truth test.
● The iterator is applied in parallel, meaning the first iterator to return true will fire the detect callback
with that result
Detecting
var async = require('async');
function filter(item, callback) {
callback(Math.pow(item, 2) > 10);
}
async.detect([1, 2, 3, 4, 5], filter, function(result)
{
console.log('Square value is greater than 10: %j', result);
});
Output : Square value is greater than 10: 4
Detecting
var async = require('async');
var fs = require(‘fs’);
async.detect(['file1','file2','file3'], fs.exists, function(results){
console.log("exist : ", result);
});

More Related Content

PDF
Java 8 - functional features
PDF
Monads in Swift
PDF
ECMAScript 6 Review
PDF
Austin Bingham. Transducers in Python. PyCon Belarus
PDF
Think Async: Asynchronous Patterns in NodeJS
PPTX
Flying Futures at the same sky can make the sun rise at midnight
PDF
Cocoa heads 09112017
DOCX
Java 8 - functional features
Monads in Swift
ECMAScript 6 Review
Austin Bingham. Transducers in Python. PyCon Belarus
Think Async: Asynchronous Patterns in NodeJS
Flying Futures at the same sky can make the sun rise at midnight
Cocoa heads 09112017

What's hot (20)

PPTX
Recursion in c++
PDF
Slot shifting1
PDF
Continuations
PDF
Advanced functional programing in Swift
PPTX
functions of C++
PPT
functions
PPTX
PDF
Pure Future
PPTX
Java script
PDF
DSU C&C++ Practical File Diploma
PDF
M11 operator overloading and type conversion
PPTX
Lambda Expressions in C++
PPTX
LinkedIn TBC JavaScript 100: Functions
PDF
The Ring programming language version 1.3 book - Part 59 of 88
PDF
Java Practical File Diploma
PPTX
MATLAB Scripts - Examples
PDF
Berlin meetup
PPTX
Operator Overloading & Type Conversions
PDF
Understanding Javascript Engine to Code Better
PPTX
Functions in C++
Recursion in c++
Slot shifting1
Continuations
Advanced functional programing in Swift
functions of C++
functions
Pure Future
Java script
DSU C&C++ Practical File Diploma
M11 operator overloading and type conversion
Lambda Expressions in C++
LinkedIn TBC JavaScript 100: Functions
The Ring programming language version 1.3 book - Part 59 of 88
Java Practical File Diploma
MATLAB Scripts - Examples
Berlin meetup
Operator Overloading & Type Conversions
Understanding Javascript Engine to Code Better
Functions in C++
Ad

Similar to Node js (20)

PPTX
Avoiding Callback Hell with Async.js
PDF
Promises - Asynchronous Control Flow
PDF
Asynchronous programming done right - Node.js
PDF
Asynchronous programming with java script and node.js
PDF
Flow control in node.js
PDF
Callbacks, promises, generators - asynchronous javascript
PPTX
Async discussion 9_29_15
PDF
Async History - javascript
PDF
Asynchronous development in JavaScript
PDF
Intro to Asynchronous Javascript
PPTX
Ecma script
PDF
The art of concurrent programming
PPTX
node.js workshop- JavaScript Async
PDF
Promise: async programming hero
PDF
Kamil witecki asynchronous, yet readable, code
PDF
All you need to know about Callbacks, Promises, Generators
KEY
Node.js - Best practices
KEY
Playing With Fire - An Introduction to Node.js
PPTX
Node.js Patterns for Discerning Developers
PDF
JavaScript Promises Simplified [Free Meetup]
Avoiding Callback Hell with Async.js
Promises - Asynchronous Control Flow
Asynchronous programming done right - Node.js
Asynchronous programming with java script and node.js
Flow control in node.js
Callbacks, promises, generators - asynchronous javascript
Async discussion 9_29_15
Async History - javascript
Asynchronous development in JavaScript
Intro to Asynchronous Javascript
Ecma script
The art of concurrent programming
node.js workshop- JavaScript Async
Promise: async programming hero
Kamil witecki asynchronous, yet readable, code
All you need to know about Callbacks, Promises, Generators
Node.js - Best practices
Playing With Fire - An Introduction to Node.js
Node.js Patterns for Discerning Developers
JavaScript Promises Simplified [Free Meetup]
Ad

More from LearningTech (20)

PPTX
PPTX
PostCss
PPTX
ReactJs
PPTX
Docker
PPTX
Semantic ui
PPTX
node.js errors
PPTX
Process control nodejs
PPTX
Expression tree
PPTX
SQL 效能調校
PPTX
flexbox report
PPTX
Vic weekly learning_20160504
PPTX
Reflection & activator
PPTX
Peggy markdown
PPTX
Node child process
PPTX
20160415ken.lee
PPTX
Peggy elasticsearch應用
PPTX
Expression tree
PPTX
Vic weekly learning_20160325
PPTX
D3js learning tips
PPTX
git command
PostCss
ReactJs
Docker
Semantic ui
node.js errors
Process control nodejs
Expression tree
SQL 效能調校
flexbox report
Vic weekly learning_20160504
Reflection & activator
Peggy markdown
Node child process
20160415ken.lee
Peggy elasticsearch應用
Expression tree
Vic weekly learning_20160325
D3js learning tips
git command

Recently uploaded (20)

PPTX
ACSFv1EN-58255 AWS Academy Cloud Security Foundations.pptx
PPTX
VMware vSphere Foundation How to Sell Presentation-Ver1.4-2-14-2024.pptx
PDF
Architecting across the Boundaries of two Complex Domains - Healthcare & Tech...
PDF
Building Integrated photovoltaic BIPV_UPV.pdf
PDF
Encapsulation_ Review paper, used for researhc scholars
PDF
Advanced methodologies resolving dimensionality complications for autism neur...
PPTX
Spectroscopy.pptx food analysis technology
PPTX
20250228 LYD VKU AI Blended-Learning.pptx
PDF
Agricultural_Statistics_at_a_Glance_2022_0.pdf
PDF
MIND Revenue Release Quarter 2 2025 Press Release
PDF
How UI/UX Design Impacts User Retention in Mobile Apps.pdf
PDF
Spectral efficient network and resource selection model in 5G networks
PPTX
Effective Security Operations Center (SOC) A Modern, Strategic, and Threat-In...
PPTX
Understanding_Digital_Forensics_Presentation.pptx
PDF
Mobile App Security Testing_ A Comprehensive Guide.pdf
PDF
Optimiser vos workloads AI/ML sur Amazon EC2 et AWS Graviton
PDF
NewMind AI Weekly Chronicles - August'25 Week I
PDF
Per capita expenditure prediction using model stacking based on satellite ima...
PDF
cuic standard and advanced reporting.pdf
PDF
TokAI - TikTok AI Agent : The First AI Application That Analyzes 10,000+ Vira...
ACSFv1EN-58255 AWS Academy Cloud Security Foundations.pptx
VMware vSphere Foundation How to Sell Presentation-Ver1.4-2-14-2024.pptx
Architecting across the Boundaries of two Complex Domains - Healthcare & Tech...
Building Integrated photovoltaic BIPV_UPV.pdf
Encapsulation_ Review paper, used for researhc scholars
Advanced methodologies resolving dimensionality complications for autism neur...
Spectroscopy.pptx food analysis technology
20250228 LYD VKU AI Blended-Learning.pptx
Agricultural_Statistics_at_a_Glance_2022_0.pdf
MIND Revenue Release Quarter 2 2025 Press Release
How UI/UX Design Impacts User Retention in Mobile Apps.pdf
Spectral efficient network and resource selection model in 5G networks
Effective Security Operations Center (SOC) A Modern, Strategic, and Threat-In...
Understanding_Digital_Forensics_Presentation.pptx
Mobile App Security Testing_ A Comprehensive Guide.pdf
Optimiser vos workloads AI/ML sur Amazon EC2 et AWS Graviton
NewMind AI Weekly Chronicles - August'25 Week I
Per capita expenditure prediction using model stacking based on satellite ima...
cuic standard and advanced reporting.pdf
TokAI - TikTok AI Agent : The First AI Application That Analyzes 10,000+ Vira...

Node js

  • 1. Controlling the Callback Flow Jason NodeJs
  • 2. Controlling the Callback Flow ● Understanding the boomerang effect ● Using async to control callback flow Executing in Series Executing in Parallel Cascading Queuing Iterating Mapping Reducing
  • 3. A demonstration of the boomerang effect. var fs = require('fs'); function append_some_a_to_b(callback) { fs.open(__dirname + '/a.txt', 'r', function(err, aFd) { var buffer = new Buffer(10); fs.read(aFd, buffer, 0, buffer.length, 0, function(err) { fs.close(aFd, function(err) { fs.open(__dirname + '/b.txt', 'a', function(err, bFd) { fs.fstat(bFd, function(err, bStats) { fs.write(bFd, buffer, 0, buffer.length, bStats.size, function(err) { fs.close(bFd, callback); }); }); }); }); }); }); }
  • 4. Using async to control callback flow ● To install async you include it in your package.json manifest or simply install it on your application root directory like this: > npm install async ● The async module comes with a series of helper functions that allow you to perform asynchronous iteration and flow control.
  • 5. Executing in Series series(tasks, [callback]) ● Run the functions in the tasks array in series, each one running once the previous function has completed. ● If any functions in the series pass an error to its callback, no more functions are run, and callback is immediately called with the value of the error ● tasks - An array or object containing functions to run, each function is passed a callback(err, result) it must call on completion with an error err (which can be null) and an optional result value ● callback - Callback receives an array of results when tasks have completed.
  • 6. Executing in Series var async = require('async'); async.series([ function(callback){ callback(null, 'one'); }, function(callback){ callback(null, 'two'); } ], function(err, results){ if (err) { throw err; } console.log('results: %j', results); // results is now equal to ['one', 'two'] });
  • 7. Executing in Series var async = require('async'); async.series({ one: function(callback){ setTimeout(function(){ callback(null, 1); }, 200); }, two: function(callback){ setTimeout(function(){ callback(null, 2); }, 100); } }, function(err, results) { if (err) { throw err; } console.log('results: %j', results); // results is now equal to: {one: 1, two: 2} });
  • 8. Executing in Parallel parallel(tasks, [callback]) ● Run the tasks array of functions in parallel, without waiting until the previous function has completed ● If any functions in the series pass an error to its callback, no more functions are run, and callback is immediately called with the value of the error ● tasks - An array or object containing functions to run. Each function is passed a callback(err, result) which it must call on completion with an error err (which can be null) and an optional result value. ● callback(err, results) - An optional callback to run once all the functions have completed. This function gets a results array (or object) containing all the result arguments passed to the task
  • 9. Executing in Parallel var async = require('async'); async.parallel([ function(callback){ setTimeout(function(){ callback(null, 'one'); }, 200); }, function(callback){ setTimeout(function(){ callback(null, 'two'); }, 100); } ], function(err, results) { if (err) { throw err; } console.log('results: %j', results); // results is now equal to: {one: 1, two: 2} });
  • 10. Executing in Parallel var async = require('async'); async.parallel({ one: function(callback){ setTimeout(function(){ callback(null, 1); }, 200); }, two: function(callback){ setTimeout(function(){ callback(null, 2); }, 100); } }, function(err, results) { if (err) { throw err; } console.log('results: %j', results); // results is now equal to: {one: 1, two: 2} });
  • 11. Cascading waterfall(tasks, [callback]) ● Runs the tasks array of functions in series, each passing their results to the next in the array. ● If any of the tasks pass an error to their own callback, the next function is not executed, and the main callback is immediately called with the error. ● tasks - An array of functions to run, each function is passed a callback(err, result1, result2, ...) it must call on completion. The first argument is an error (which can be null) and any further arguments will be passed as arguments in order to the next task. ● callback(err, [results]) - An optional callback to run once all the functions have completed. This will be passed the results of the last task's callback.
  • 12. Cascading var async = require('async'); async.waterfall([ function(callback){ callback(null, 'one', 'two'); }, function(arg1, arg2, callback){ callback(null, 'three'); }, function(arg1, callback){ callback(null, 'done'); } ], function (err, result) { console.log('result : ', result); });
  • 13. Queuing queue(worker, concurrency) ● If you need to perform some repetitive asynchronous jobs and you want to control the concurrency ● worker(task, callback) - An asynchronous function for processing a queued task, which must call its callback(err) argument when finished, with an optional error as an argument. ● concurrency - An integer for determining how many worker functions should be run in parallel.
  • 14. Queuing var async = require('async'); var q = async.queue(function (task, callback) { console.log('hello ' + task.name); callback(); }, 2); q.drain = function() { console.log('all items have been processed'); } q.push( {name: 'foo'}, function (err) { console.log('finished processing foo'); }); q.push( {name: 'bar'}, function (err) { console.log('finished processing bar'); }); q.push( [{name: 'baz'},{name: 'bay'},{name: 'bax'}] , function (err) { console.log('finished processing item'); }); q.unshift( {name: 'bar'}, function (err) { console.log('finished processing bar'); });
  • 15. Iterating forEach(arr, iterator, callback) ● If you use this async.forEach function, you are invoking an iterator for each element in parallel ● iterator(item, callback) - A function to apply to each item in arr. ● callback(results) - A callback which is called when all iterator functions have finished, or an error occurs.
  • 16. Iterating var async = require('async'); var results = {}; function iterator(value, callback) { results[value] = Math.pow(value, 2); callback(); } async.forEach([1, 2, 3, 4], iterator, function(err){ if (err) { throw err; } console.log('results: %j', results); }); Output : results: {“1”: 1, “2” : 4, “3” : 9, “4” : 16}
  • 17. Mapping map(arr, iterator, callback) ● Produces a new array of values by mapping each value in arr through the iterator function ● iterator(item, callback) - A function to apply to each item in arr. The iterator is passed a callback(err, transformed) which must be called once it has completed with an error (which can be null) and a transformed item. ● callback(err, results) - A callback which is called when all iterator functions have finished, or an error occurs. Results is an array of the transformed items from the arr.
  • 18. Mapping var async = require('async'); function iterator(value, callback) { callback(null, Math.pow(value, 2)); } async.map([1, 2, 3, 4], iterator, function(err, results) { if (err) { throw err; } console.log('results: %j', results); }); Output : results : [1,4,9,16]
  • 19. Reducing reduce(arr, memo, iterator, callback) ● Reduces array into a single value using an async iterator to return each successive step. ● memo - The initial state of the reduction. ● iterator(memo, item, callback) - A function applied to each item in the array to produce the next step in the reduction. The iterator is passed a callback(err, reduction) which accepts an optional error as its first argument, and the state of the reduction as the second. ● callback(err, result) - A callback which is called after all the iterator functions have finished. Result is the reduced value.
  • 20. Reducing var async = require('async'); async.reduce([1,2,3], 0, function(memo, item, callback){ console.log(‘memo:‘ + memo, ‘item:’ + item); callback(null, memo + item); }, function(err, result){ console.log('Result : %j', result); }); Output : memo : 0 item : 1 memo : 1 item : 2 memo : 3 item : 3 Result : 6
  • 21. Filtering filter(arr, iterator, callback) ● Returns a new array of all the values in array which pass an async truth test. ● The callback for each iterator call only accepts a single argument of true or false ● It does not accept an error argument first!
  • 22. Filtering var async = require('async'); function filter(item, callback) { callback(Math.pow(item, 2) > 10); } async.filter([1, 2, 3, 4, 5], filter, function(results) { console.log('Square value is greater than 10: %j', results); }); Output : Square value is greater than 10: [4,5]
  • 23. Filtering var async = require('async'); var fs = require(‘fs’); async.filter(['file1','file2','file3'], fs.exists, function(results){ // results now equals an array of the existing files });
  • 24. Detecting detect(arr, iterator, callback) ● Returns the first value in array that passes an async truth test. ● The iterator is applied in parallel, meaning the first iterator to return true will fire the detect callback with that result
  • 25. Detecting var async = require('async'); function filter(item, callback) { callback(Math.pow(item, 2) > 10); } async.detect([1, 2, 3, 4, 5], filter, function(result) { console.log('Square value is greater than 10: %j', result); }); Output : Square value is greater than 10: 4
  • 26. Detecting var async = require('async'); var fs = require(‘fs’); async.detect(['file1','file2','file3'], fs.exists, function(results){ console.log("exist : ", result); });