SlideShare a Scribd company logo
#wdc13
Put On Your Asynchronous
Hat and Node
Marc Fasel @marcfasel
Shine Technologies http://blog.shinetech.com
1Saturday, 4 May 13
#wdc13
What is an Asynchronous Hat?
2Saturday, 4 May 13
#wdc13
Node.js
Server-side JavaScript platform
Event-driven, non-blocking I/O
All I/O is asynchronous
Asynchrononous everywhere
3Saturday, 4 May 13
#wdc13
Different Mindsets
Synchronous coding comes natural
Natural execution order
Asynchronous coding a bit harder
Serial execution leads to nesting
New: parallel execution
Single Callback functions and Event Emitters
Explicit error handling
4Saturday, 4 May 13
#wdc13
Synchronous Code
var filenames = fs.readdirSync('/tmp/');
for (var i = 0; i < filenames.length; i++) {
console.log(filenames[i]);
}
console.log('Done.');
5Saturday, 4 May 13
#wdc13
Asynchronous Code
fs.readdir('/tmp/', function (err, filenames) {
for (var i = 0; i < filenames.length; i++) {
console.log(filenames[i]);
}
console.log('Done.');
});
6Saturday, 4 May 13
#wdc13
Some More Synchronous Code
var totalBytes = 0;
for (var i = 0; i < filenames.length; i ++) {
var stats = fs.statSync('/tmp/' + filenames[i]);
totalBytes += stats.size;
}
console.log('Total bytes: ' + totalBytes);
7Saturday, 4 May 13
#wdc13
Asynchronous Parallel Execution
var count = filenames.length;
for (var i = 0; i < filenames.length; i++) {
! fs.stat('/tmp/' + filenames[i], function (err, stats) {
! ! totalBytes += stats.size;
! ! count--;
! ! if (count === 0) {
! ! ! // We’re done
! ! ! console.log('Total bytes: ' + totalBytes);
! ! }
! });
};
8Saturday, 4 May 13
#wdc13
fs.readFile(dirFrom+filename,'utf-8', function (err, data){
! ! if (err) return callback(err);
! ! fs.writeFile(dirTo+filename,data,'utf-8', function (err){
! ! ! ! if (err) return callback(err);
! ! ! ! fs.chmod(dirTo+filename,0777, function (err){
! ! ! ! ! ! if (err) return callback(err);
! ! ! ! ! ! anotherAsyncFunction(param1, function (err){
! ! ! ! ! ! ! ! if (err) return callback(err);
! ! ! ! ! ! ! ! anotherAsyncFunction(param1, function (err){
! ! ! ! ! ! ! ! })
! ! ! ! ! ! })
! ! ! ! })
! ! })
});
Pyramid of Doom
9Saturday, 4 May 13
#wdc13
Event Mechanisms
Events used for asynchronous communication
Two types of asynchronous functions:
Single Callback:  one ‘done’ event, single callback function
Event Emitter:  more than one event
10Saturday, 4 May 13
#wdc13
Single Callback Functions
fs.readdir(dir, function (err, filenames) {
// Failure
! if (err) return callback(err);
// Success
! console.log(dir + ' has ' + filenames.length + 'files.');
});!
11Saturday, 4 May 13
#wdc13
Event Emitter
var readStream = fs.createReadStream(filename);
readStream.on('open', function () {
! readStream.pipe(res);
});
readStream.on('error', function(err) {
! // Failure
! res.end(err);
});
12Saturday, 4 May 13
#wdc13
Error Handling
Synchronous
Exceptions give us lots of goodies
Asynchronous
Explicit error code everywhere
Lots of boilerplate
More Pyramid of Doom
13Saturday, 4 May 13
#wdc13
Synchronous Error Handling
We are blessed with Exceptions
Stop execution immediately
Automatically bubble up call hierarchy
Error code logically separated from application code
14Saturday, 4 May 13
#wdc13
World With Exceptions
function readFile(fileName) {
! var file = openFile(fileName);
! var data = readContent(file);
! closeFile(file);
! return data;
}
function readContent(file) {
! throw new Error("Exception!");
}
try {
! var myData = readFile(fileName);
} catch (e) {
! // Handle error!
}
15Saturday, 4 May 13
#wdc13
World Before Exceptions
function readFile(err, filePath) {
! var file = openFile(err, filePath);
! if (err) { return err; }
! var data = readContent(err, file);
! if (err) { return err;}
! closeFile(err, file);
! if (err) { return err; }
! return data;
}
var myFileContent = readFile(err,filePath);
if (err) { // Handle error };
16Saturday, 4 May 13
#wdc13
Asynchronous Error Handling
try {
fs.readFile('/tmp/test.txt', 'utf-8', function (err, data) {
!if (err) {
!! throw err;
!}
console.log(data);
});!
} catch (e) {
! console.log(e);
}
Exceptions possible
try/catch does not have desired result
17Saturday, 4 May 13
#wdc13
Callback Error Handling
fs.readFile('/tmp/test.txt', 'utf-8', function (err, data) {
! if (err) return callback(err);
! // Success
! console.log(data);
});!
18Saturday, 4 May 13
#wdc13
Event Emitter Error Handling
var readStream = fs.createReadStream(filename);
readStream.on('open', function () {
! readStream.pipe(res);
});
readStream.on('error', function(err) {
res.end(err);
});
19Saturday, 4 May 13
#wdc13
Pyramid of Doom With Error Handling
fs.readFile(dirFrom+filename,'utf-8', function (err, data){
! ! if (err) return callback(err);
! ! fs.writeFile(dirTo+filename,data,'utf-8', function (err){
! ! ! ! if (err) return callback(err);
! ! ! ! fs.chmod(dirTo+filename,0777, function (err){
! ! ! ! ! ! if (err) return callback(err);
! ! ! ! ! ! anotherAsyncFunction(param1, function (err){
! ! ! ! ! ! ! ! if (err) return callback(err);
! ! ! ! ! ! ! ! anotherAsyncFunction(param1, function (err){
! ! ! ! ! ! ! ! ! if (err) return callback(err);
! ! ! ! ! ! ! ! ! callback();
! ! ! ! ! ! ! ! })
! ! ! ! ! ! })
! ! ! ! })
! ! })
});
20Saturday, 4 May 13
#wdc13
Asynchronous Error Handling
Can’t catch exceptions that happen in callbacks
Explicit code to handle error
Simple Callback: error code gets mixed with application code
Event Emitter: error code separate
21Saturday, 4 May 13
#wdc13
Help is Available
22Saturday, 4 May 13
#wdc13
Named Callbacks
fs.readdir('/tmp/', readDirCallback);
function readDirCallback (err, filenames) {
for (var i = 0; i < filenames.length; i++) {
console.log(filenames[i]);
}
console.log('Done.');
};
23Saturday, 4 May 13
#wdc13
Nesting Named Callbacks
function copyFile(filePathFrom, filePathTo, callback){
! fs.readFile(filePathFrom,'utf-8', readFileCallback);
! function readFileCallback(err, data){
! ! if (err) return callback(err);
! ! fs.writeFile(filePathTo, data, 'utf-8', writeFileCallback);
! ! function writeFileCallback(err){
! ! ! if (err) return callback(err);
! ! ! callback();
! ! };
! };
};
24Saturday, 4 May 13
#wdc13
Control Flow libraries
Fix problems with asynchronous Control Flow
Control flow first thing people want to fix
Many control flow libraries available
25Saturday, 4 May 13
#wdc13
Async.js
The Underscore.js of asynchronous code
Control flow constructs
Functional constructs
26Saturday, 4 May 13
#wdc13
Series
async.series([
! function(callback) {
! ! fs.chmod(file1,0777, function (err){
! ! ! if (err) return callback(err);
! ! ! callback ();
! ! });
! },
! function(callback) {
! ! fs.chmod(file2,0777, function (err){
! ! ! if (err) return callback(err);
! ! ! callback();
! ! });
! }],
! function done(err) {
! ! if (err) return console.log(err);
! ! console.log ('Done.');
! }
);
27Saturday, 4 May 13
#wdc13
Parallel
async.parallel([
! function(callback) {
! ! fs.chmod(file1,0777, function (err){
! ! ! if (err) return callback(err);
! ! ! callback ();
! ! });
! },
! function(callback) {
! ! fs.chmod(file2,0777, function (err){
! ! ! if (err) return callback(err);
! ! ! callback();
! ! });
! }],
! function done(err) {
! ! if (err) return console.log(err);
! ! console.log ('Done.');
! }
);
28Saturday, 4 May 13
#wdc13
Functional Constructs
async.map(['file1','file2','file3'], fs.stat, function(err, results){
// results is now an array of stats for each file
});
async.filter(['file1','file2','file3'], path.exists, function(results){
// results now equals an array of the existing files
});
async.reduce(...);
29Saturday, 4 May 13
#wdc13
Promises
Pattern for asynchronous control flow
Promises are objects representing a future outcome of an
asynchronous call
Outcome will be either ‘resolved’ or ‘rejected’
Attach callbacks to ‘resolved’, ‘rejected’
30Saturday, 4 May 13
#wdc13
Callback -> Promise
function getReadFilePromise(){
! var deferred = q.defer();
! fs.readFile('/tmp/test.txt', 'utf-8', function (err, data) {
! ! if (err) { deferred.reject(err); }
! ! deferred.resolve(data);
! });! !
! return deferred.promise;
}
31Saturday, 4 May 13
#wdc13
Promises
var readFilePromise = getReadFilePromise();
readFilePromise.then(
function(data){ console.log('Resolved:' + data); },
function(err){ console.log('Rejected:' + err); });
32Saturday, 4 May 13
#wdc13
Promises
Cool Stuff: Promise Chaining
33Saturday, 4 May 13
#wdc13
Promise Chaining
readFilePromise(filePathFrom)
! .then(writeFilePromise(filePathTo))
! .then(changeFilePermissionPromise(filePathTo,'777'))
! .then(oneMorePromise())
! .then(oneMorePromise());
var promises = [
! readFilePromise,
! writeFilePromise,
! changeFilePermissionPromise,
! oneMorePromise,
! oneMorePromise
];
var allPromise = Q.all(promises);
34Saturday, 4 May 13
#wdc13
Promise Error Handling
readFilePromise(filePathFrom)
! .then(writeFilePromise(filePathTo))
! .then(changeFilePermissionPromise(filePathTo,'777'),
! ! errorCallback);
35Saturday, 4 May 13
#wdc13
Promises
Promises help with asynchronous control flow
Avoid the Pyramid of Doom
Exception style error bubbling
36Saturday, 4 May 13
#wdc13
Conclusion
Asynchronous programming needs an asynchronous hat
New things
Callbacks
Event Emitters
Explicit error handling
For the more difficult stuff
Named callbacks
Async.js
Promises
37Saturday, 4 May 13
#wdc13
Thank you.
38Saturday, 4 May 13
#wdc13
References
Trevor Burnham - Async JavaScript: Build More Responsive Apps with
Less Code
Pedro Teixeira - Professional Node.js: Building Javascript-Based
Scalable Software
You’re Missing the Point of Promises - http://domenic.me/2012/10/14/
youre-missing-the-point-of-promises/
Popular Control Flow Libraries - http://dailyjs.com/2011/11/14/popular-
control-flow/
39Saturday, 4 May 13

More Related Content

What's hot

Linux-Fu for PHP Developers
Linux-Fu for PHP DevelopersLinux-Fu for PHP Developers
Linux-Fu for PHP Developers
Lorna Mitchell
 
Redis as a message queue
Redis as a message queueRedis as a message queue
Redis as a message queue
Brandon Lamb
 
Shell Script
Shell ScriptShell Script
Shell Script
Adam Victor Brandizzi
 
PL/Perl - New Features in PostgreSQL 9.0
PL/Perl - New Features in PostgreSQL 9.0PL/Perl - New Features in PostgreSQL 9.0
PL/Perl - New Features in PostgreSQL 9.0
Tim Bunce
 
Oliver hookins puppetcamp2011
Oliver hookins puppetcamp2011Oliver hookins puppetcamp2011
Oliver hookins puppetcamp2011
Puppet
 
KubeCon EU 2016: Custom Volume Plugins
KubeCon EU 2016: Custom Volume PluginsKubeCon EU 2016: Custom Volume Plugins
KubeCon EU 2016: Custom Volume Plugins
KubeAcademy
 
Memory Manglement in Raku
Memory Manglement in RakuMemory Manglement in Raku
Memory Manglement in Raku
Workhorse Computing
 
PL/Perl - New Features in PostgreSQL 9.0 201012
PL/Perl - New Features in PostgreSQL 9.0 201012PL/Perl - New Features in PostgreSQL 9.0 201012
PL/Perl - New Features in PostgreSQL 9.0 201012
Tim Bunce
 
Working with databases in Perl
Working with databases in PerlWorking with databases in Perl
Working with databases in Perl
Laurent Dami
 
tit
tittit
Automating Disaster Recovery PostgreSQL
Automating Disaster Recovery PostgreSQLAutomating Disaster Recovery PostgreSQL
Automating Disaster Recovery PostgreSQL
Nina Kaufman
 
Shell Script Tutorial
Shell Script TutorialShell Script Tutorial
Shell Script Tutorial
Quang Minh Đoàn
 
Overloading Perl OPs using XS
Overloading Perl OPs using XSOverloading Perl OPs using XS
Overloading Perl OPs using XS
ℕicolas ℝ.
 
Hadoop spark performance comparison
Hadoop spark performance comparisonHadoop spark performance comparison
Hadoop spark performance comparison
arunkumar sadhasivam
 
various tricks for remote linux exploits  by Seok-Ha Lee (wh1ant)
various tricks for remote linux exploits  by Seok-Ha Lee (wh1ant)various tricks for remote linux exploits  by Seok-Ha Lee (wh1ant)
various tricks for remote linux exploits  by Seok-Ha Lee (wh1ant)
CODE BLUE
 
Module 03 Programming on Linux
Module 03 Programming on LinuxModule 03 Programming on Linux
Module 03 Programming on Linux
Tushar B Kute
 
Application Logging in the 21st century - 2014.key
Application Logging in the 21st century - 2014.keyApplication Logging in the 21st century - 2014.key
Application Logging in the 21st century - 2014.key
Tim Bunce
 
OSBConf 2015 | Backups with rdiff backup and rsnapshot by christoph mitasch &...
OSBConf 2015 | Backups with rdiff backup and rsnapshot by christoph mitasch &...OSBConf 2015 | Backups with rdiff backup and rsnapshot by christoph mitasch &...
OSBConf 2015 | Backups with rdiff backup and rsnapshot by christoph mitasch &...
NETWAYS
 
Workshop on command line tools - day 2
Workshop on command line tools - day 2Workshop on command line tools - day 2
Workshop on command line tools - day 2
Leandro Lima
 
Unix cheatsheet
Unix cheatsheetUnix cheatsheet
Unix cheatsheet
Dhaval Shukla
 

What's hot (20)

Linux-Fu for PHP Developers
Linux-Fu for PHP DevelopersLinux-Fu for PHP Developers
Linux-Fu for PHP Developers
 
Redis as a message queue
Redis as a message queueRedis as a message queue
Redis as a message queue
 
Shell Script
Shell ScriptShell Script
Shell Script
 
PL/Perl - New Features in PostgreSQL 9.0
PL/Perl - New Features in PostgreSQL 9.0PL/Perl - New Features in PostgreSQL 9.0
PL/Perl - New Features in PostgreSQL 9.0
 
Oliver hookins puppetcamp2011
Oliver hookins puppetcamp2011Oliver hookins puppetcamp2011
Oliver hookins puppetcamp2011
 
KubeCon EU 2016: Custom Volume Plugins
KubeCon EU 2016: Custom Volume PluginsKubeCon EU 2016: Custom Volume Plugins
KubeCon EU 2016: Custom Volume Plugins
 
Memory Manglement in Raku
Memory Manglement in RakuMemory Manglement in Raku
Memory Manglement in Raku
 
PL/Perl - New Features in PostgreSQL 9.0 201012
PL/Perl - New Features in PostgreSQL 9.0 201012PL/Perl - New Features in PostgreSQL 9.0 201012
PL/Perl - New Features in PostgreSQL 9.0 201012
 
Working with databases in Perl
Working with databases in PerlWorking with databases in Perl
Working with databases in Perl
 
tit
tittit
tit
 
Automating Disaster Recovery PostgreSQL
Automating Disaster Recovery PostgreSQLAutomating Disaster Recovery PostgreSQL
Automating Disaster Recovery PostgreSQL
 
Shell Script Tutorial
Shell Script TutorialShell Script Tutorial
Shell Script Tutorial
 
Overloading Perl OPs using XS
Overloading Perl OPs using XSOverloading Perl OPs using XS
Overloading Perl OPs using XS
 
Hadoop spark performance comparison
Hadoop spark performance comparisonHadoop spark performance comparison
Hadoop spark performance comparison
 
various tricks for remote linux exploits  by Seok-Ha Lee (wh1ant)
various tricks for remote linux exploits  by Seok-Ha Lee (wh1ant)various tricks for remote linux exploits  by Seok-Ha Lee (wh1ant)
various tricks for remote linux exploits  by Seok-Ha Lee (wh1ant)
 
Module 03 Programming on Linux
Module 03 Programming on LinuxModule 03 Programming on Linux
Module 03 Programming on Linux
 
Application Logging in the 21st century - 2014.key
Application Logging in the 21st century - 2014.keyApplication Logging in the 21st century - 2014.key
Application Logging in the 21st century - 2014.key
 
OSBConf 2015 | Backups with rdiff backup and rsnapshot by christoph mitasch &...
OSBConf 2015 | Backups with rdiff backup and rsnapshot by christoph mitasch &...OSBConf 2015 | Backups with rdiff backup and rsnapshot by christoph mitasch &...
OSBConf 2015 | Backups with rdiff backup and rsnapshot by christoph mitasch &...
 
Workshop on command line tools - day 2
Workshop on command line tools - day 2Workshop on command line tools - day 2
Workshop on command line tools - day 2
 
Unix cheatsheet
Unix cheatsheetUnix cheatsheet
Unix cheatsheet
 

Similar to Put on Your Asynchronous Hat and Node

NodeJs
NodeJsNodeJs
NodeJs
dizabl
 
Node Powered Mobile
Node Powered MobileNode Powered Mobile
Node Powered Mobile
Tim Caswell
 
Node intro
Node introNode intro
Node intro
cloudhead
 
Vagrant for real
Vagrant for realVagrant for real
Vagrant for real
Michele Orselli
 
Programming language for the cloud infrastructure
Programming language for the cloud infrastructureProgramming language for the cloud infrastructure
Programming language for the cloud infrastructure
Yaroslav Muravskyi
 
Sequential Async Call
Sequential Async CallSequential Async Call
Sequential Async Call
Sirius Fang
 
Introduction to Kernel Programming
Introduction to Kernel ProgrammingIntroduction to Kernel Programming
Introduction to Kernel Programming
Ahmed Mekkawy
 
Kamil witecki asynchronous, yet readable, code
Kamil witecki asynchronous, yet readable, codeKamil witecki asynchronous, yet readable, code
Kamil witecki asynchronous, yet readable, code
Kamil Witecki
 
Post Exploitation Bliss: Loading Meterpreter on a Factory iPhone, Black Hat U...
Post Exploitation Bliss: Loading Meterpreter on a Factory iPhone, Black Hat U...Post Exploitation Bliss: Loading Meterpreter on a Factory iPhone, Black Hat U...
Post Exploitation Bliss: Loading Meterpreter on a Factory iPhone, Black Hat U...
Vincenzo Iozzo
 
Node js lecture
Node js lectureNode js lecture
Node js lecture
Darryl Sherman
 
The why and how of moving to PHP 5.4/5.5
The why and how of moving to PHP 5.4/5.5The why and how of moving to PHP 5.4/5.5
The why and how of moving to PHP 5.4/5.5
Wim Godden
 
Real Time Web with Node
Real Time Web with NodeReal Time Web with Node
Real Time Web with Node
Tim Caswell
 
Writing Swift code with great testability
Writing Swift code with great testabilityWriting Swift code with great testability
Writing Swift code with great testability
John Sundell
 
PHP CLI: A Cinderella Story
PHP CLI: A Cinderella StoryPHP CLI: A Cinderella Story
PHP CLI: A Cinderella Story
Mike Lively
 
Transforming WebSockets
Transforming WebSocketsTransforming WebSockets
Transforming WebSockets
Arnout Kazemier
 
Keeping it small - Getting to know the Slim PHP micro framework
Keeping it small - Getting to know the Slim PHP micro frameworkKeeping it small - Getting to know the Slim PHP micro framework
Keeping it small - Getting to know the Slim PHP micro framework
Jeremy Kendall
 
Asynchronous programming done right - Node.js
Asynchronous programming done right - Node.jsAsynchronous programming done right - Node.js
Asynchronous programming done right - Node.js
Piotr Pelczar
 
On secure application of PHP wrappers
On secure application  of PHP wrappersOn secure application  of PHP wrappers
On secure application of PHP wrappers
Positive Hack Days
 
Node Boot Camp
Node Boot CampNode Boot Camp
Node Boot Camp
Troy Miles
 
JSLab. Домников Виталий. "ES6 генераторы и Koa.js"
JSLab. Домников Виталий. "ES6 генераторы и Koa.js"JSLab. Домников Виталий. "ES6 генераторы и Koa.js"
JSLab. Домников Виталий. "ES6 генераторы и Koa.js"
GeeksLab Odessa
 

Similar to Put on Your Asynchronous Hat and Node (20)

NodeJs
NodeJsNodeJs
NodeJs
 
Node Powered Mobile
Node Powered MobileNode Powered Mobile
Node Powered Mobile
 
Node intro
Node introNode intro
Node intro
 
Vagrant for real
Vagrant for realVagrant for real
Vagrant for real
 
Programming language for the cloud infrastructure
Programming language for the cloud infrastructureProgramming language for the cloud infrastructure
Programming language for the cloud infrastructure
 
Sequential Async Call
Sequential Async CallSequential Async Call
Sequential Async Call
 
Introduction to Kernel Programming
Introduction to Kernel ProgrammingIntroduction to Kernel Programming
Introduction to Kernel Programming
 
Kamil witecki asynchronous, yet readable, code
Kamil witecki asynchronous, yet readable, codeKamil witecki asynchronous, yet readable, code
Kamil witecki asynchronous, yet readable, code
 
Post Exploitation Bliss: Loading Meterpreter on a Factory iPhone, Black Hat U...
Post Exploitation Bliss: Loading Meterpreter on a Factory iPhone, Black Hat U...Post Exploitation Bliss: Loading Meterpreter on a Factory iPhone, Black Hat U...
Post Exploitation Bliss: Loading Meterpreter on a Factory iPhone, Black Hat U...
 
Node js lecture
Node js lectureNode js lecture
Node js lecture
 
The why and how of moving to PHP 5.4/5.5
The why and how of moving to PHP 5.4/5.5The why and how of moving to PHP 5.4/5.5
The why and how of moving to PHP 5.4/5.5
 
Real Time Web with Node
Real Time Web with NodeReal Time Web with Node
Real Time Web with Node
 
Writing Swift code with great testability
Writing Swift code with great testabilityWriting Swift code with great testability
Writing Swift code with great testability
 
PHP CLI: A Cinderella Story
PHP CLI: A Cinderella StoryPHP CLI: A Cinderella Story
PHP CLI: A Cinderella Story
 
Transforming WebSockets
Transforming WebSocketsTransforming WebSockets
Transforming WebSockets
 
Keeping it small - Getting to know the Slim PHP micro framework
Keeping it small - Getting to know the Slim PHP micro frameworkKeeping it small - Getting to know the Slim PHP micro framework
Keeping it small - Getting to know the Slim PHP micro framework
 
Asynchronous programming done right - Node.js
Asynchronous programming done right - Node.jsAsynchronous programming done right - Node.js
Asynchronous programming done right - Node.js
 
On secure application of PHP wrappers
On secure application  of PHP wrappersOn secure application  of PHP wrappers
On secure application of PHP wrappers
 
Node Boot Camp
Node Boot CampNode Boot Camp
Node Boot Camp
 
JSLab. Домников Виталий. "ES6 генераторы и Koa.js"
JSLab. Домников Виталий. "ES6 генераторы и Koa.js"JSLab. Домников Виталий. "ES6 генераторы и Koa.js"
JSLab. Домников Виталий. "ES6 генераторы и Koa.js"
 

Recently uploaded

Programming Foundation Models with DSPy - Meetup Slides
Programming Foundation Models with DSPy - Meetup SlidesProgramming Foundation Models with DSPy - Meetup Slides
Programming Foundation Models with DSPy - Meetup Slides
Zilliz
 
dbms calicut university B. sc Cs 4th sem.pdf
dbms  calicut university B. sc Cs 4th sem.pdfdbms  calicut university B. sc Cs 4th sem.pdf
dbms calicut university B. sc Cs 4th sem.pdf
Shinana2
 
Trusted Execution Environment for Decentralized Process Mining
Trusted Execution Environment for Decentralized Process MiningTrusted Execution Environment for Decentralized Process Mining
Trusted Execution Environment for Decentralized Process Mining
LucaBarbaro3
 
Artificial Intelligence for XMLDevelopment
Artificial Intelligence for XMLDevelopmentArtificial Intelligence for XMLDevelopment
Artificial Intelligence for XMLDevelopment
Octavian Nadolu
 
AWS Cloud Cost Optimization Presentation.pptx
AWS Cloud Cost Optimization Presentation.pptxAWS Cloud Cost Optimization Presentation.pptx
AWS Cloud Cost Optimization Presentation.pptx
HarisZaheer8
 
Letter and Document Automation for Bonterra Impact Management (fka Social Sol...
Letter and Document Automation for Bonterra Impact Management (fka Social Sol...Letter and Document Automation for Bonterra Impact Management (fka Social Sol...
Letter and Document Automation for Bonterra Impact Management (fka Social Sol...
Jeffrey Haguewood
 
GenAI Pilot Implementation in the organizations
GenAI Pilot Implementation in the organizationsGenAI Pilot Implementation in the organizations
GenAI Pilot Implementation in the organizations
kumardaparthi1024
 
Nunit vs XUnit vs MSTest Differences Between These Unit Testing Frameworks.pdf
Nunit vs XUnit vs MSTest Differences Between These Unit Testing Frameworks.pdfNunit vs XUnit vs MSTest Differences Between These Unit Testing Frameworks.pdf
Nunit vs XUnit vs MSTest Differences Between These Unit Testing Frameworks.pdf
flufftailshop
 
Driving Business Innovation: Latest Generative AI Advancements & Success Story
Driving Business Innovation: Latest Generative AI Advancements & Success StoryDriving Business Innovation: Latest Generative AI Advancements & Success Story
Driving Business Innovation: Latest Generative AI Advancements & Success Story
Safe Software
 
Your One-Stop Shop for Python Success: Top 10 US Python Development Providers
Your One-Stop Shop for Python Success: Top 10 US Python Development ProvidersYour One-Stop Shop for Python Success: Top 10 US Python Development Providers
Your One-Stop Shop for Python Success: Top 10 US Python Development Providers
akankshawande
 
Serial Arm Control in Real Time Presentation
Serial Arm Control in Real Time PresentationSerial Arm Control in Real Time Presentation
Serial Arm Control in Real Time Presentation
tolgahangng
 
TrustArc Webinar - 2024 Global Privacy Survey
TrustArc Webinar - 2024 Global Privacy SurveyTrustArc Webinar - 2024 Global Privacy Survey
TrustArc Webinar - 2024 Global Privacy Survey
TrustArc
 
System Design Case Study: Building a Scalable E-Commerce Platform - Hiike
System Design Case Study: Building a Scalable E-Commerce Platform - HiikeSystem Design Case Study: Building a Scalable E-Commerce Platform - Hiike
System Design Case Study: Building a Scalable E-Commerce Platform - Hiike
Hiike
 
Ocean lotus Threat actors project by John Sitima 2024 (1).pptx
Ocean lotus Threat actors project by John Sitima 2024 (1).pptxOcean lotus Threat actors project by John Sitima 2024 (1).pptx
Ocean lotus Threat actors project by John Sitima 2024 (1).pptx
SitimaJohn
 
Salesforce Integration for Bonterra Impact Management (fka Social Solutions A...
Salesforce Integration for Bonterra Impact Management (fka Social Solutions A...Salesforce Integration for Bonterra Impact Management (fka Social Solutions A...
Salesforce Integration for Bonterra Impact Management (fka Social Solutions A...
Jeffrey Haguewood
 
Nordic Marketo Engage User Group_June 13_ 2024.pptx
Nordic Marketo Engage User Group_June 13_ 2024.pptxNordic Marketo Engage User Group_June 13_ 2024.pptx
Nordic Marketo Engage User Group_June 13_ 2024.pptx
MichaelKnudsen27
 
Building Production Ready Search Pipelines with Spark and Milvus
Building Production Ready Search Pipelines with Spark and MilvusBuilding Production Ready Search Pipelines with Spark and Milvus
Building Production Ready Search Pipelines with Spark and Milvus
Zilliz
 
Digital Marketing Trends in 2024 | Guide for Staying Ahead
Digital Marketing Trends in 2024 | Guide for Staying AheadDigital Marketing Trends in 2024 | Guide for Staying Ahead
Digital Marketing Trends in 2024 | Guide for Staying Ahead
Wask
 
leewayhertz.com-AI in predictive maintenance Use cases technologies benefits ...
leewayhertz.com-AI in predictive maintenance Use cases technologies benefits ...leewayhertz.com-AI in predictive maintenance Use cases technologies benefits ...
leewayhertz.com-AI in predictive maintenance Use cases technologies benefits ...
alexjohnson7307
 
How to Interpret Trends in the Kalyan Rajdhani Mix Chart.pdf
How to Interpret Trends in the Kalyan Rajdhani Mix Chart.pdfHow to Interpret Trends in the Kalyan Rajdhani Mix Chart.pdf
How to Interpret Trends in the Kalyan Rajdhani Mix Chart.pdf
Chart Kalyan
 

Recently uploaded (20)

Programming Foundation Models with DSPy - Meetup Slides
Programming Foundation Models with DSPy - Meetup SlidesProgramming Foundation Models with DSPy - Meetup Slides
Programming Foundation Models with DSPy - Meetup Slides
 
dbms calicut university B. sc Cs 4th sem.pdf
dbms  calicut university B. sc Cs 4th sem.pdfdbms  calicut university B. sc Cs 4th sem.pdf
dbms calicut university B. sc Cs 4th sem.pdf
 
Trusted Execution Environment for Decentralized Process Mining
Trusted Execution Environment for Decentralized Process MiningTrusted Execution Environment for Decentralized Process Mining
Trusted Execution Environment for Decentralized Process Mining
 
Artificial Intelligence for XMLDevelopment
Artificial Intelligence for XMLDevelopmentArtificial Intelligence for XMLDevelopment
Artificial Intelligence for XMLDevelopment
 
AWS Cloud Cost Optimization Presentation.pptx
AWS Cloud Cost Optimization Presentation.pptxAWS Cloud Cost Optimization Presentation.pptx
AWS Cloud Cost Optimization Presentation.pptx
 
Letter and Document Automation for Bonterra Impact Management (fka Social Sol...
Letter and Document Automation for Bonterra Impact Management (fka Social Sol...Letter and Document Automation for Bonterra Impact Management (fka Social Sol...
Letter and Document Automation for Bonterra Impact Management (fka Social Sol...
 
GenAI Pilot Implementation in the organizations
GenAI Pilot Implementation in the organizationsGenAI Pilot Implementation in the organizations
GenAI Pilot Implementation in the organizations
 
Nunit vs XUnit vs MSTest Differences Between These Unit Testing Frameworks.pdf
Nunit vs XUnit vs MSTest Differences Between These Unit Testing Frameworks.pdfNunit vs XUnit vs MSTest Differences Between These Unit Testing Frameworks.pdf
Nunit vs XUnit vs MSTest Differences Between These Unit Testing Frameworks.pdf
 
Driving Business Innovation: Latest Generative AI Advancements & Success Story
Driving Business Innovation: Latest Generative AI Advancements & Success StoryDriving Business Innovation: Latest Generative AI Advancements & Success Story
Driving Business Innovation: Latest Generative AI Advancements & Success Story
 
Your One-Stop Shop for Python Success: Top 10 US Python Development Providers
Your One-Stop Shop for Python Success: Top 10 US Python Development ProvidersYour One-Stop Shop for Python Success: Top 10 US Python Development Providers
Your One-Stop Shop for Python Success: Top 10 US Python Development Providers
 
Serial Arm Control in Real Time Presentation
Serial Arm Control in Real Time PresentationSerial Arm Control in Real Time Presentation
Serial Arm Control in Real Time Presentation
 
TrustArc Webinar - 2024 Global Privacy Survey
TrustArc Webinar - 2024 Global Privacy SurveyTrustArc Webinar - 2024 Global Privacy Survey
TrustArc Webinar - 2024 Global Privacy Survey
 
System Design Case Study: Building a Scalable E-Commerce Platform - Hiike
System Design Case Study: Building a Scalable E-Commerce Platform - HiikeSystem Design Case Study: Building a Scalable E-Commerce Platform - Hiike
System Design Case Study: Building a Scalable E-Commerce Platform - Hiike
 
Ocean lotus Threat actors project by John Sitima 2024 (1).pptx
Ocean lotus Threat actors project by John Sitima 2024 (1).pptxOcean lotus Threat actors project by John Sitima 2024 (1).pptx
Ocean lotus Threat actors project by John Sitima 2024 (1).pptx
 
Salesforce Integration for Bonterra Impact Management (fka Social Solutions A...
Salesforce Integration for Bonterra Impact Management (fka Social Solutions A...Salesforce Integration for Bonterra Impact Management (fka Social Solutions A...
Salesforce Integration for Bonterra Impact Management (fka Social Solutions A...
 
Nordic Marketo Engage User Group_June 13_ 2024.pptx
Nordic Marketo Engage User Group_June 13_ 2024.pptxNordic Marketo Engage User Group_June 13_ 2024.pptx
Nordic Marketo Engage User Group_June 13_ 2024.pptx
 
Building Production Ready Search Pipelines with Spark and Milvus
Building Production Ready Search Pipelines with Spark and MilvusBuilding Production Ready Search Pipelines with Spark and Milvus
Building Production Ready Search Pipelines with Spark and Milvus
 
Digital Marketing Trends in 2024 | Guide for Staying Ahead
Digital Marketing Trends in 2024 | Guide for Staying AheadDigital Marketing Trends in 2024 | Guide for Staying Ahead
Digital Marketing Trends in 2024 | Guide for Staying Ahead
 
leewayhertz.com-AI in predictive maintenance Use cases technologies benefits ...
leewayhertz.com-AI in predictive maintenance Use cases technologies benefits ...leewayhertz.com-AI in predictive maintenance Use cases technologies benefits ...
leewayhertz.com-AI in predictive maintenance Use cases technologies benefits ...
 
How to Interpret Trends in the Kalyan Rajdhani Mix Chart.pdf
How to Interpret Trends in the Kalyan Rajdhani Mix Chart.pdfHow to Interpret Trends in the Kalyan Rajdhani Mix Chart.pdf
How to Interpret Trends in the Kalyan Rajdhani Mix Chart.pdf
 

Put on Your Asynchronous Hat and Node

  • 1. #wdc13 Put On Your Asynchronous Hat and Node Marc Fasel @marcfasel Shine Technologies http://blog.shinetech.com 1Saturday, 4 May 13
  • 2. #wdc13 What is an Asynchronous Hat? 2Saturday, 4 May 13
  • 3. #wdc13 Node.js Server-side JavaScript platform Event-driven, non-blocking I/O All I/O is asynchronous Asynchrononous everywhere 3Saturday, 4 May 13
  • 4. #wdc13 Different Mindsets Synchronous coding comes natural Natural execution order Asynchronous coding a bit harder Serial execution leads to nesting New: parallel execution Single Callback functions and Event Emitters Explicit error handling 4Saturday, 4 May 13
  • 5. #wdc13 Synchronous Code var filenames = fs.readdirSync('/tmp/'); for (var i = 0; i < filenames.length; i++) { console.log(filenames[i]); } console.log('Done.'); 5Saturday, 4 May 13
  • 6. #wdc13 Asynchronous Code fs.readdir('/tmp/', function (err, filenames) { for (var i = 0; i < filenames.length; i++) { console.log(filenames[i]); } console.log('Done.'); }); 6Saturday, 4 May 13
  • 7. #wdc13 Some More Synchronous Code var totalBytes = 0; for (var i = 0; i < filenames.length; i ++) { var stats = fs.statSync('/tmp/' + filenames[i]); totalBytes += stats.size; } console.log('Total bytes: ' + totalBytes); 7Saturday, 4 May 13
  • 8. #wdc13 Asynchronous Parallel Execution var count = filenames.length; for (var i = 0; i < filenames.length; i++) { ! fs.stat('/tmp/' + filenames[i], function (err, stats) { ! ! totalBytes += stats.size; ! ! count--; ! ! if (count === 0) { ! ! ! // We’re done ! ! ! console.log('Total bytes: ' + totalBytes); ! ! } ! }); }; 8Saturday, 4 May 13
  • 9. #wdc13 fs.readFile(dirFrom+filename,'utf-8', function (err, data){ ! ! if (err) return callback(err); ! ! fs.writeFile(dirTo+filename,data,'utf-8', function (err){ ! ! ! ! if (err) return callback(err); ! ! ! ! fs.chmod(dirTo+filename,0777, function (err){ ! ! ! ! ! ! if (err) return callback(err); ! ! ! ! ! ! anotherAsyncFunction(param1, function (err){ ! ! ! ! ! ! ! ! if (err) return callback(err); ! ! ! ! ! ! ! ! anotherAsyncFunction(param1, function (err){ ! ! ! ! ! ! ! ! }) ! ! ! ! ! ! }) ! ! ! ! }) ! ! }) }); Pyramid of Doom 9Saturday, 4 May 13
  • 10. #wdc13 Event Mechanisms Events used for asynchronous communication Two types of asynchronous functions: Single Callback:  one ‘done’ event, single callback function Event Emitter:  more than one event 10Saturday, 4 May 13
  • 11. #wdc13 Single Callback Functions fs.readdir(dir, function (err, filenames) { // Failure ! if (err) return callback(err); // Success ! console.log(dir + ' has ' + filenames.length + 'files.'); });! 11Saturday, 4 May 13
  • 12. #wdc13 Event Emitter var readStream = fs.createReadStream(filename); readStream.on('open', function () { ! readStream.pipe(res); }); readStream.on('error', function(err) { ! // Failure ! res.end(err); }); 12Saturday, 4 May 13
  • 13. #wdc13 Error Handling Synchronous Exceptions give us lots of goodies Asynchronous Explicit error code everywhere Lots of boilerplate More Pyramid of Doom 13Saturday, 4 May 13
  • 14. #wdc13 Synchronous Error Handling We are blessed with Exceptions Stop execution immediately Automatically bubble up call hierarchy Error code logically separated from application code 14Saturday, 4 May 13
  • 15. #wdc13 World With Exceptions function readFile(fileName) { ! var file = openFile(fileName); ! var data = readContent(file); ! closeFile(file); ! return data; } function readContent(file) { ! throw new Error("Exception!"); } try { ! var myData = readFile(fileName); } catch (e) { ! // Handle error! } 15Saturday, 4 May 13
  • 16. #wdc13 World Before Exceptions function readFile(err, filePath) { ! var file = openFile(err, filePath); ! if (err) { return err; } ! var data = readContent(err, file); ! if (err) { return err;} ! closeFile(err, file); ! if (err) { return err; } ! return data; } var myFileContent = readFile(err,filePath); if (err) { // Handle error }; 16Saturday, 4 May 13
  • 17. #wdc13 Asynchronous Error Handling try { fs.readFile('/tmp/test.txt', 'utf-8', function (err, data) { !if (err) { !! throw err; !} console.log(data); });! } catch (e) { ! console.log(e); } Exceptions possible try/catch does not have desired result 17Saturday, 4 May 13
  • 18. #wdc13 Callback Error Handling fs.readFile('/tmp/test.txt', 'utf-8', function (err, data) { ! if (err) return callback(err); ! // Success ! console.log(data); });! 18Saturday, 4 May 13
  • 19. #wdc13 Event Emitter Error Handling var readStream = fs.createReadStream(filename); readStream.on('open', function () { ! readStream.pipe(res); }); readStream.on('error', function(err) { res.end(err); }); 19Saturday, 4 May 13
  • 20. #wdc13 Pyramid of Doom With Error Handling fs.readFile(dirFrom+filename,'utf-8', function (err, data){ ! ! if (err) return callback(err); ! ! fs.writeFile(dirTo+filename,data,'utf-8', function (err){ ! ! ! ! if (err) return callback(err); ! ! ! ! fs.chmod(dirTo+filename,0777, function (err){ ! ! ! ! ! ! if (err) return callback(err); ! ! ! ! ! ! anotherAsyncFunction(param1, function (err){ ! ! ! ! ! ! ! ! if (err) return callback(err); ! ! ! ! ! ! ! ! anotherAsyncFunction(param1, function (err){ ! ! ! ! ! ! ! ! ! if (err) return callback(err); ! ! ! ! ! ! ! ! ! callback(); ! ! ! ! ! ! ! ! }) ! ! ! ! ! ! }) ! ! ! ! }) ! ! }) }); 20Saturday, 4 May 13
  • 21. #wdc13 Asynchronous Error Handling Can’t catch exceptions that happen in callbacks Explicit code to handle error Simple Callback: error code gets mixed with application code Event Emitter: error code separate 21Saturday, 4 May 13
  • 23. #wdc13 Named Callbacks fs.readdir('/tmp/', readDirCallback); function readDirCallback (err, filenames) { for (var i = 0; i < filenames.length; i++) { console.log(filenames[i]); } console.log('Done.'); }; 23Saturday, 4 May 13
  • 24. #wdc13 Nesting Named Callbacks function copyFile(filePathFrom, filePathTo, callback){ ! fs.readFile(filePathFrom,'utf-8', readFileCallback); ! function readFileCallback(err, data){ ! ! if (err) return callback(err); ! ! fs.writeFile(filePathTo, data, 'utf-8', writeFileCallback); ! ! function writeFileCallback(err){ ! ! ! if (err) return callback(err); ! ! ! callback(); ! ! }; ! }; }; 24Saturday, 4 May 13
  • 25. #wdc13 Control Flow libraries Fix problems with asynchronous Control Flow Control flow first thing people want to fix Many control flow libraries available 25Saturday, 4 May 13
  • 26. #wdc13 Async.js The Underscore.js of asynchronous code Control flow constructs Functional constructs 26Saturday, 4 May 13
  • 27. #wdc13 Series async.series([ ! function(callback) { ! ! fs.chmod(file1,0777, function (err){ ! ! ! if (err) return callback(err); ! ! ! callback (); ! ! }); ! }, ! function(callback) { ! ! fs.chmod(file2,0777, function (err){ ! ! ! if (err) return callback(err); ! ! ! callback(); ! ! }); ! }], ! function done(err) { ! ! if (err) return console.log(err); ! ! console.log ('Done.'); ! } ); 27Saturday, 4 May 13
  • 28. #wdc13 Parallel async.parallel([ ! function(callback) { ! ! fs.chmod(file1,0777, function (err){ ! ! ! if (err) return callback(err); ! ! ! callback (); ! ! }); ! }, ! function(callback) { ! ! fs.chmod(file2,0777, function (err){ ! ! ! if (err) return callback(err); ! ! ! callback(); ! ! }); ! }], ! function done(err) { ! ! if (err) return console.log(err); ! ! console.log ('Done.'); ! } ); 28Saturday, 4 May 13
  • 29. #wdc13 Functional Constructs async.map(['file1','file2','file3'], fs.stat, function(err, results){ // results is now an array of stats for each file }); async.filter(['file1','file2','file3'], path.exists, function(results){ // results now equals an array of the existing files }); async.reduce(...); 29Saturday, 4 May 13
  • 30. #wdc13 Promises Pattern for asynchronous control flow Promises are objects representing a future outcome of an asynchronous call Outcome will be either ‘resolved’ or ‘rejected’ Attach callbacks to ‘resolved’, ‘rejected’ 30Saturday, 4 May 13
  • 31. #wdc13 Callback -> Promise function getReadFilePromise(){ ! var deferred = q.defer(); ! fs.readFile('/tmp/test.txt', 'utf-8', function (err, data) { ! ! if (err) { deferred.reject(err); } ! ! deferred.resolve(data); ! });! ! ! return deferred.promise; } 31Saturday, 4 May 13
  • 32. #wdc13 Promises var readFilePromise = getReadFilePromise(); readFilePromise.then( function(data){ console.log('Resolved:' + data); }, function(err){ console.log('Rejected:' + err); }); 32Saturday, 4 May 13
  • 33. #wdc13 Promises Cool Stuff: Promise Chaining 33Saturday, 4 May 13
  • 34. #wdc13 Promise Chaining readFilePromise(filePathFrom) ! .then(writeFilePromise(filePathTo)) ! .then(changeFilePermissionPromise(filePathTo,'777')) ! .then(oneMorePromise()) ! .then(oneMorePromise()); var promises = [ ! readFilePromise, ! writeFilePromise, ! changeFilePermissionPromise, ! oneMorePromise, ! oneMorePromise ]; var allPromise = Q.all(promises); 34Saturday, 4 May 13
  • 35. #wdc13 Promise Error Handling readFilePromise(filePathFrom) ! .then(writeFilePromise(filePathTo)) ! .then(changeFilePermissionPromise(filePathTo,'777'), ! ! errorCallback); 35Saturday, 4 May 13
  • 36. #wdc13 Promises Promises help with asynchronous control flow Avoid the Pyramid of Doom Exception style error bubbling 36Saturday, 4 May 13
  • 37. #wdc13 Conclusion Asynchronous programming needs an asynchronous hat New things Callbacks Event Emitters Explicit error handling For the more difficult stuff Named callbacks Async.js Promises 37Saturday, 4 May 13
  • 39. #wdc13 References Trevor Burnham - Async JavaScript: Build More Responsive Apps with Less Code Pedro Teixeira - Professional Node.js: Building Javascript-Based Scalable Software You’re Missing the Point of Promises - http://domenic.me/2012/10/14/ youre-missing-the-point-of-promises/ Popular Control Flow Libraries - http://dailyjs.com/2011/11/14/popular- control-flow/ 39Saturday, 4 May 13