When starting to dabble with Javascript, the biggest challenge for most developers is understanding how to deal with asynchronous development. During this talk, we will cover some of the different ways to handle async programming like callbacks, promises, generators, async/away and events. As we cover those, we will also plunge into some of the mechanics of the NodeJs engine, namely the event loop. Developers attending this talk will have a better understanding of asynchronous programming and will have a few new tools to their belt to tackle those issues as they come.
25. @joel__lord
#CodeMash
PB&J Sandwich
• Get the ingredients
‣ Get some bread
‣ Get some PB
‣ Get some J
• Prepare the sandwich
‣ Spread PB
‣ Spread J
• Close it
• Eat it!
37. @joel__lord
#CodeMash
Classic Architecture
(PHP, Java)
• I want a sandwich!
• Process start
• Fetches the
ingredients
• A new customer arrives:
I want a sandwich!
• Another process starts
• Fetches the
ingredients
•
!
🥪
🍞
🥜
🍓
🥪
!
🥪
🍞
38. @joel__lord
#CodeMash
Classic Architecture
(PHP, Java)
• I want a sandwich!
• Process start
• Fetches the
ingredients
• A new customer arrives:
I want a sandwich!
• Another process starts
• Fetches the
ingredients
• Prepares the
sandwich
•
!
🥪
🍞
🥜
🍓
🥪
!
🥪
🍞
🥜
!
🥪
39. @joel__lord
#CodeMash
Classic Architecture
(PHP, Java)
• I want a sandwich!
• Process start
• Fetches the
ingredients
• A new customer arrives:
I want a sandwich!
• Another process starts
• Fetches the
ingredients
• Prepares the
sandwich
• Here’s your
sandwich !
🥪
🍞
🥜
🍓
🥪
!
🥪
🍞
🥜
🍓
!
🥪
!
🥪
40. @joel__lord
#CodeMash
Classic Architecture
(PHP, Java)
• I want a sandwich!
• Process start
• Fetches the
ingredients
• A new customer arrives:
I want a sandwich!
• Another process starts
• Fetches the
ingredients
• Prepares the
sandwich
• Here’s your
sandwich !
🥪
🍞
🥜
🍓
🥪
!
🥪
🍞
🥜
🍓
!
🥪
🍞
!
🥪
41. @joel__lord
#CodeMash
Classic Architecture
(PHP, Java)
• I want a sandwich!
• Process start
• Fetches the
ingredients
• A new customer arrives:
I want a sandwich!
• Another process starts
• Fetches the
ingredients
• Prepares the
sandwich
• Here’s your
sandwich !
🥪
🍞
🥜
🍓
🥪
!
🥪
🍞
🥜
🍓
🥪
!
🥪
🍞
🥜
!
🥪
🍞
42. @joel__lord
#CodeMash
Classic Architecture
(PHP, Java)
• I want a sandwich!
• Process start
• Fetches the
ingredients
• A new customer arrives:
I want a sandwich!
• Another process starts
• Fetches the
ingredients
• Prepares the
sandwich
• Here’s your
sandwich !
🥪
🍞
🥜
🍓
🥪
!
🥪
🍞
🥜
🍓
🥪
!
🥪
🍞
🥜
🍓
!
🥪
🍞
🥜
43. @joel__lord
#CodeMash
Classic Architecture
(PHP, Java)
• I want a sandwich!
• Process start
• Fetches the
ingredients
• A new customer arrives:
I want a sandwich!
• Another process starts
• Fetches the
ingredients
• Prepares the
sandwich
• Here’s your
sandwich !
🥪
🍞
🥜
🍓
🥪
!
🥪
🍞
🥜
🍓
🥪
!
🥪
🍞
🥜
🍓
!
🥪
🍞
🥜
44. @joel__lord
#CodeMash
Classic Architecture
(PHP, Java)
• I want a sandwich!
• Process start
• Fetches the
ingredients
• A new customer arrives:
I want a sandwich!
• Another process starts
• Fetches the
ingredients
• Prepares the
sandwich
• Here’s your
sandwich !
🥪
🍞
🥜
🍓
🥪
!
🥪
🍞
🥜
🍓
🥪
!
🥪
🍞
🥜
🍓
🥪
!
🥪
🍞
🥜
🍓
45. @joel__lord
#CodeMash
Classic Architecture
(PHP, Java)
• I want a sandwich!
• Process start
• Fetches the
ingredients
• A new customer arrives:
I want a sandwich!
• Another process starts
• Fetches the
ingredients
• Prepares the
sandwich
• Here’s your
sandwich !
🥪
🍞
🥜
🍓
🥪
!
🥪
🍞
🥜
🍓
🥪
!
🥪
🍞
🥜
🍓
🥪
!
🥪
🍞
🥜
🍓
46. @joel__lord
#CodeMash
Classic Architecture
(PHP, Java)
• I want a sandwich!
• Process start
• Fetches the
ingredients
• A new customer arrives:
I want a sandwich!
• Another process starts
• Fetches the
ingredients
• Prepares the
sandwich
• Here’s your
sandwich !
🥪
🍞
🥜
🍓
🥪
!
🥪
🍞
🥜
🍓
🥪
!
🥪
🍞
🥜
🍓
🥪
!
🥪
🍞
🥜
🍓
🥪
47. @joel__lord
#CodeMash
Event Loop
(NodeJS)
• I want a sandwich!
• Sure
• “Worker, Make a
sandwich”
• A new customer arrives:
I want a sandwich!
• Sure
• “Other worker,
make a sandwich”
• Here is your sandwich
customer 1
!
🥪
48. @joel__lord
#CodeMash
Event Loop
(NodeJS)
• I want a sandwich!
• Sure
• “Worker, Make a
sandwich”
• A new customer arrives:
I want a sandwich!
• Sure
• “Other worker,
make a sandwich”
• Here is your sandwich
customer 1
!
🥪
🍞
49. @joel__lord
#CodeMash
Event Loop
(NodeJS)
• I want a sandwich!
• Sure
• “Worker, Make a
sandwich”
• A new customer arrives:
I want a sandwich!
• Sure
• “Other worker,
make a sandwich”
• Here is your sandwich
customer 1
!
🥪
🍞
🥜
50. @joel__lord
#CodeMash
Event Loop
(NodeJS)
• I want a sandwich!
• Sure
• “Worker, Make a
sandwich”
• A new customer arrives:
I want a sandwich!
• Sure
• “Other worker,
make a sandwich”
• Here is your sandwich
customer 1
!
🥪
🍞
🥜
🍓
51. @joel__lord
#CodeMash
Event Loop
(NodeJS)
• I want a sandwich!
• Sure
• “Worker, Make a
sandwich”
• A new customer arrives:
I want a sandwich!
• Sure
• “Other worker,
make a sandwich”
• Here is your sandwich
customer 1
!
🥪
🍞
🥜
🍓
52. @joel__lord
#CodeMash
Event Loop
(NodeJS)
• I want a sandwich!
• Sure
• “Worker, Make a
sandwich”
• A new customer arrives:
I want a sandwich!
• Sure
• “Other worker,
make a sandwich”
• Here is your sandwich
customer 1
!
🥪
🍞
🥜
🍓
🥪
53. @joel__lord
#CodeMash
Event Loop
(NodeJS)
• I want a sandwich!
• Sure
• “Worker, Make a
sandwich”
• A new customer arrives:
I want a sandwich!
• Sure
• “Other worker,
make a sandwich”
• Here is your sandwich
customer 1
!
🥪
🍞
🥜
🍓
🥪
🥪
54. @joel__lord
#CodeMash
Event Loop
(NodeJS)
• I want a sandwich!
• Sure
• “Worker, Make a
sandwich”
• A new customer arrives:
I want a sandwich!
• Sure
• “Other worker,
make a sandwich”
• Here is your sandwich
customer 1
!
🥪
🍞
🥜
🍓
🥪
!
🥪
55. @joel__lord
#CodeMash
Event Loop
(NodeJS)
• I want a sandwich!
• Sure
• “Worker, Make a
sandwich”
• A new customer arrives:
I want a sandwich!
• Sure
• “Other worker,
make a sandwich”
• Here is your sandwich
customer 1
!
🥪
🍞
🥜
🍓
🥪
🍞
!
🥪
🥪
56. @joel__lord
#CodeMash
Event Loop
(NodeJS)
• I want a sandwich!
• Sure
• “Worker, Make a
sandwich”
• A new customer arrives:
I want a sandwich!
• Sure
• “Other worker,
make a sandwich”
• Here is your sandwich
customer 1
!
🥪
🍞
🥜
🍓
🥪
🍞
!
🥪
🥪
🥜
57. @joel__lord
#CodeMash
Event Loop
(NodeJS)
• I want a sandwich!
• Sure
• “Worker, Make a
sandwich”
• A new customer arrives:
I want a sandwich!
• Sure
• “Other worker,
make a sandwich”
• Here is your sandwich
customer 1
!
🥪
🍞
🥜
🍓
🥪
🍞
!
🥪
🥪
🥜
!
🥪
🍓
🍞
58. @joel__lord
#CodeMash
Event Loop
(NodeJS)
• I want a sandwich!
• Sure
• “Worker, Make a
sandwich”
• A new customer arrives:
I want a sandwich!
• Sure
• “Other worker,
make a sandwich”
• Here is your sandwich
customer 1
!
🥪
🍞
🥜
🍓
🥪
🍞
!
🥪
🥪
🥜
!
🥪
🍓
🍞
🥜
59. @joel__lord
#CodeMash
Event Loop
(NodeJS)
• I want a sandwich!
• Sure
• “Worker, Make a
sandwich”
• A new customer arrives:
I want a sandwich!
• Sure
• “Other worker,
make a sandwich”
• Here is your sandwich
customer 1
!
🥪
🍞
🥜
🍓
🥪
🍞
!
🥪
🥪
🥜
!
🥪
🍓
🍞
🥜
🥪
🍓
60. @joel__lord
#CodeMash
Event Loop
(NodeJS)
• I want a sandwich!
• Sure
• “Worker, Make a
sandwich”
• A new customer arrives:
I want a sandwich!
• Sure
• “Other worker,
make a sandwich”
• Here is your sandwich
customer 1
!
🥪
🍞
🥜
🍓
🥪
🍞
!
🥪
🥪
🥜
!
🥪
🍓
🍞
🥜
🥪
🍓
🥪
61. @joel__lord
#CodeMash
Event Loop
(NodeJS)
• I want a sandwich!
• Sure
• “Worker, Make a
sandwich”
• A new customer arrives:
I want a sandwich!
• Sure
• “Other worker,
make a sandwich”
• Here is your sandwich
customer 1
!
🥪
🍞
🥜
🍓
🥪
🍞
!
🥪
🥪
🥜
!
🥪
🍓
🍞
🥜
🥪
🍓
🥪
🥪
62. @joel__lord
#CodeMash
Event Loop
(NodeJS)
• I want a sandwich!
• Sure
• “Worker, Make a
sandwich”
• A new customer arrives:
I want a sandwich!
• Sure
• “Other worker,
make a sandwich”
• Here is your sandwich
customer 1
!
🥪
🍞
🥜
🍓
🥪
🍞
🥜
🍓
🥪
🍞
🥜
🍓
🥪
!
🥪
!
🥪
🥪
🥪
🥪
67. @joel__lord
#CodeMash
What is a callback
! Simply a function as
an argument to a
function
! Could be
synchronous or
asynchronous
// //Synchronous
let array = [1,2,3];
console.log("begin");
array.map((i) => {console.log(i)});
console.log("after");
68. @joel__lord
#CodeMash
What is a callback
! Simply a function as
an argument to a
function
! Could be
synchronous or
asynchronous
69. @joel__lord
#CodeMash
What is a callback
! Simply a function as
an argument to a
function
! Could be
synchronous or
asynchronous
//Asynchronous
console.log("begin");
fs.readFile("./cb.txt", (err, data) => {
console.log(“File content”);
});
console.log("after");
70. @joel__lord
#CodeMash
What is a callback
! Simply a function as
an argument to a
function
! Could be
synchronous or
asynchronous
71. @joel__lord
#CodeMash
What is a callback
! Simply a function as
an argument to a
function
! Could be
synchronous or
asynchronous
//Asynchronous
console.log("begin");
fs.readFile("./cb.txt", (err, data) => {
console.log(“File content”);
});
console.log("after");
72. @joel__lord
#CodeMash
What is a callback
! Simply a function as
an argument to a
function
! Could be
synchronous or
asynchronous
//Asynchronous
.....................
....................... (err, data) => {
console.log(“File content”);
}..
.....................
73. @joel__lord
#CodeMash
What is a callback
Asynchronous callback and the event loop
console.log("begin");
fs.readFile("./cb.txt",
(err, data) => {
console.log(data);
});
console.log("after");
74. @joel__lord
#CodeMash
What is a callback
Asynchronous callback and the event loop
fs.readFile("./cb.txt",
(err, data) => {
console.log(data);
});
console.log("after");
75. @joel__lord
#CodeMash
What is a callback
Asynchronous callback and the event loop
console.log("after"); (err, data) => {
console.log(data);
}
96. @joel__lord
#CodeMash
What is a promise?
! A representation of a
callback
! Returns a .then()
method
! Error handling
simplified
//Asynchronous
console.log("begin");
asyncPromise().then(onSuccess, onFailure);
console.log("after");
97. @joel__lord
#CodeMash
What is a promise?
! A representation of a
callback
! Returns a .then()
method
! Error handling
simplified
//Asynchronous
console.log("begin");
fs.readFile("./cb.txt")
.then((data) => {
console.log(data);
}, (err) => {
console.log(err);
});
console.log("after");
98. @joel__lord
#CodeMash
What is a promise?
! A representation of a
callback
! Returns a .then()
method
! Error handling
simplified
//Asynchronous
console.log("begin");
fs.readFile("./cb.txt")
.then((data) => {
console.log(data);
}).catch((err) => {
console.log(err);
});
console.log("after");
120. @joel__lord
#CodeMash
What is a generator?
! An iterable function
! “Pauses” the
execution of a
function
function* iterable() {
let i = 4;
while(i--) {
yield i;
}
}
let iteration = iterable();
console.log(iteration.next()); // 3
// Do stuff
console.log(iteration.next()); // 2
console.log(iteration.next()); // 1
console.log(iteration.next()); // 0
// More stuff
console.log(iteration.next()); // undefined
121. @joel__lord
#CodeMash
What is a generator?
! An iterable function
! “Pauses” the
execution of a
function
function* iterable() {
let i = 4;
while(i--) {
yield i;
}
}
let iteration = iterable();
console.log(iteration.next()); // 3
// Do stuff
console.log(iteration.next()); // 2
console.log(iteration.next()); // 1
console.log(iteration.next()); // 0
// More stuff
console.log(iteration.next()); // undefined
122. @joel__lord
#CodeMash
What is a generator?
! An iterable function
! “Pauses” the
execution of a
function
function* iterable() {
let i = 4;
while(i--) {
yield i;
}
}
let iteration = iterable();
console.log(iteration.next()); // 3
// Do stuff
console.log(iteration.next()); // 2
console.log(iteration.next()); // 1
console.log(iteration.next()); // 0
// More stuff
console.log(iteration.next()); // undefined
123. @joel__lord
#CodeMash
What is a generator?
! An iterable function
! “Pauses” the
execution of a
function
function* iterable() {
let i = 4;
while(i--) {
yield i;
}
}
let iteration = iterable();
console.log(iteration.next()); // 3
// Do stuff
console.log(iteration.next()); // 2
console.log(iteration.next()); // 1
console.log(iteration.next()); // 0
// More stuff
console.log(iteration.next()); // undefined
124. @joel__lord
#CodeMash
What is a generator?
! An iterable function
! “Pauses” the
execution of a
function
function* iterable() {
let i = 4;
while(i--) {
yield i;
}
}
let iteration = iterable();
console.log(iteration.next()); // 3
// Do stuff
console.log(iteration.next()); // 2
console.log(iteration.next()); // 1
console.log(iteration.next()); // 0
// More stuff
console.log(iteration.next()); // undefined
125. @joel__lord
#CodeMash
What is a generator?
! An iterable function
! “Pauses” the
execution of a
function
function* iterable() {
let i = 4;
while(i--) {
yield i;
}
}
let iteration = iterable();
console.log(iteration.next()); // 3
// Do stuff
console.log(iteration.next()); // 2
console.log(iteration.next()); // 1
console.log(iteration.next()); // 0
// More stuff
console.log(iteration.next()); // undefined
126. @joel__lord
#CodeMash
What is a generator?
! An iterable function
! “Pauses” the
execution of a
function
function* iterable() {
let i = 4;
while(i--) {
yield i;
}
}
let iteration = iterable();
console.log(iteration.next()); // 3
// Do stuff
console.log(iteration.next()); // 2
console.log(iteration.next()); // 1
console.log(iteration.next()); // 0
// More stuff
console.log(iteration.next()); // undefined
127. @joel__lord
#CodeMash
What is a generator?
! An iterable function
! “Pauses” the
execution of a
function
function* iterable() {
let i = 4;
while(i--) {
yield i;
}
}
let iteration = iterable();
console.log(iteration.next()); // 3
// Do stuff
console.log(iteration.next()); // 2
console.log(iteration.next()); // 1
console.log(iteration.next()); // 0
// More stuff
console.log(iteration.next()); // undefined
128. @joel__lord
#CodeMash
What is a generator?
! An iterable function
! “Pauses” the
execution of a
function
function* iterable() {
let i = 4;
while(i--) {
yield i;
}
}
let iteration = iterable();
console.log(iteration.next()); // 3
// Do stuff
console.log(iteration.next()); // 2
console.log(iteration.next()); // 1
console.log(iteration.next()); // 0
// More stuff
console.log(iteration.next()); // undefined
129. @joel__lord
#CodeMash
What is a generator?
! An iterable function
! “Pauses” the
execution of a
function
function* iterable() {
let i = 4;
while(i--) {
yield i;
}
}
let iteration = iterable();
console.log(iteration.next()); // 3
// Do stuff
console.log(iteration.next()); // 2
console.log(iteration.next()); // 1
console.log(iteration.next()); // 0
// More stuff
console.log(iteration.next()); // undefined
130. @joel__lord
#CodeMash
What is a generator?
! An iterable function
! “Pauses” the
execution of a
function
function* iterable() {
let i = 4;
while(i--) {
yield i;
}
}
let iteration = iterable();
console.log(iteration.next()); // 3
// Do stuff
console.log(iteration.next()); // 2
console.log(iteration.next()); // 1
console.log(iteration.next()); // 0
// More stuff
console.log(iteration.next()); // undefined
131. @joel__lord
#CodeMash
What is a generator?
! An iterable function
! “Pauses” the
execution of a
function
function* iterable() {
let i = 4;
while(i--) {
yield i;
}
}
let iteration = iterable();
console.log(iteration.next()); // 3
// Do stuff
console.log(iteration.next()); // 2
console.log(iteration.next()); // 1
console.log(iteration.next()); // 0
// More stuff
console.log(iteration.next()); // undefined
132. @joel__lord
#CodeMash
What is a generator?
! An iterable function
! “Pauses” the
execution of a
function
function* iterable() {
let i = 4;
while(i--) {
yield i;
}
}
let iteration = iterable();
console.log(iteration.next()); // 3
// Do stuff
console.log(iteration.next()); // 2
console.log(iteration.next()); // 1
console.log(iteration.next()); // 0
// More stuff
console.log(iteration.next()); // undefined
133. @joel__lord
#CodeMash
What is a generator?
! An iterable function
! “Pauses” the
execution of a
function
function* iterable() {
let i = 4;
while(i--) {
yield i;
}
}
let iteration = iterable();
console.log(iteration.next()); // 3
// Do stuff
console.log(iteration.next()); // 2
console.log(iteration.next()); // 1
console.log(iteration.next()); // 0
// More stuff
console.log(iteration.next()); // undefined
134. @joel__lord
#CodeMash
What is a generator?
! An iterable function
! “Pauses” the
execution of a
function
function* iterable() {
let i = 4;
while(i--) {
yield i;
}
}
let iteration = iterable();
console.log(iteration.next()); // 3
// Do stuff
console.log(iteration.next()); // 2
console.log(iteration.next()); // 1
console.log(iteration.next()); // 0
// More stuff
console.log(iteration.next()); // undefined
135. @joel__lord
#CodeMash
What is a generator?
! An iterable function
! “Pauses” the
execution of a
function
function* iterable() {
let i = 4;
while(i--) {
yield i;
}
}
let iteration = iterable();
console.log(iteration.next()); // 3
// Do stuff
console.log(iteration.next()); // 2
console.log(iteration.next()); // 1
console.log(iteration.next()); // 0
// More stuff
console.log(iteration.next()); // undefined
136. @joel__lord
#CodeMash
What is a generator?
! An iterable function
! “Pauses” the
execution of a
function
function* iterable() {
let i = 4;
while(i--) {
yield i;
}
}
let iteration = iterable();
console.log(iteration.next()); // 3
// Do stuff
console.log(iteration.next()); // 2
console.log(iteration.next()); // 1
console.log(iteration.next()); // 0
// More stuff
console.log(iteration.next()); // undefined
137. @joel__lord
#CodeMash
What is a generator?
! An iterable function
! “Pauses” the
execution of a
function
function* iterable() {
let i = 4;
while(i--) {
yield i;
}
}
for (let iteration of iterable()) {
console.log(iteration);
}
// 3
// 2
// 1
// 0
138. @joel__lord
#CodeMash
What is a generator?
! An iterable function
! “Pauses” the
execution of a
function
function* iterable() {
let i = 4;
while(i--) {
yield i;
}
}
for (let iteration of iterable()) {
console.log(iteration);
}
// 3
// 2
// 1
// 0
139. @joel__lord
#CodeMash
Code Result
let PBnJMaker = require("../pbnjmaker");
PBnJMaker.verbose();
function* makePBnJ() {
yield PBnJMaker.fetchBread;
yield PBnJMaker.fetchPeanutButter;
yield PBnJMaker.fetchJam;
yield PBnJMaker.spreadPeanutButter;
yield PBnJMaker.spreadJam;
yield PBnJMaker.closeSandwich;
}
for (let step of makePBnJ()) {
step();
}
console.log("Eat that sandwich");
140. @joel__lord
#CodeMash
Async Generators
! Combining the power
of promises and
generators
module.exports = (makeGenerator) => {
let generator = makeGenerator.apply(this, arguments);
function handle(result) {
// generator returns {done: [Boolean], value: [Any}}
if (result.done)
return Promise.resolve(result.value);
return
Promise.resolve(result.value).then(function(res) {
return handle(generator.next(res));
}, function(err) {
return handle(generator.throw(err));
});
}
try {
return handle(generator.next());
} catch(e) {
return Promise.reject(e);
}
};
141. @joel__lord
#CodeMash
Code Result
let PBnJMaker = require("../pbnjmaker");
let async = require("../async");
function* makePBnJ() {
yield PBnJMaker.fetchBread();
yield PBnJMaker.fetchPeanutButter();
yield PBnJMaker.fetchJam();
yield PBnJMaker.spreadPeanutButter();
yield PBnJMaker.spreadJam();
return PBnJMaker.closeSandwich();
}
async(makePBnJ).then((result) => {
console.log("Eat that " + result);
});
142. @joel__lord
#CodeMash
Code Result
let PBnJMaker = require("../pbnjmaker");
let async = require("../async");
function* makePBnJ() {
yield PBnJMaker.fetchBread();
yield PBnJMaker.fetchPeanutButter();
yield PBnJMaker.fetchJam();
yield PBnJMaker.spreadPeanutButter();
yield PBnJMaker.spreadJam();
return PBnJMaker.closeSandwich();
}
async(makePBnJ).then((result) => {
console.log("Eat that " + result);
});
175. @joel__lord
#CodeMash
Resources
More Content For You…. You Won’t Believe What Happens Next
In The Loop by Jake Archibald (video about the event loop)
http://bit.ly/event-loop-talk
Build Reactive Websites with RxJS
http://bit.ly/rxjs-book
The Awesome PBnJMaker (and other code samples)
http://bit.ly/PBnJMaker
176. Asynchronicity: concurrency. A tale of
CodeMash, Sandusky OH
January 10, 2019
@joel__lord
joellord
Special thanks to
@tomasz_ducin for the inspiration