This document discusses asynchronous programming concepts in JavaScript and PHP. It shows code examples of making asynchronous HTTP requests and file reads in both JavaScript using callbacks and PHP. The code logs messages before and after each asynchronous operation to demonstrate their non-blocking nature.
27. @joel__lord
#iJS18
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!
38. @joel__lord
#iJS18
Classic Architecture
(PHP, Java)
ā¢ I want a sandwich!
ā¢ Process start
ā¢ Fetches the
ingredients
ā¢ A new customer arrives:
I want a sandwich!
ā¢ Another process starts
ā¢
!
š„Ŗ
š
š„
š
š„Ŗ
!
š„Ŗ
39. @joel__lord
#iJS18
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
ā¢
!
š„Ŗ
š
š„
š
š„Ŗ
!
š„Ŗ
š
40. @joel__lord
#iJS18
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
ā¢
!
š„Ŗ
š
š„
š
š„Ŗ
!
š„Ŗ
š
š„
!
š„Ŗ
41. @joel__lord
#iJS18
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
#iJS18
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
#iJS18
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
#iJS18
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
#iJS18
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
#iJS18
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
#iJS18
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 !
š„Ŗ
š
š„
š
š„Ŗ
!
š„Ŗ
š
š„
š
š„Ŗ
!
š„Ŗ
š
š„
š
š„Ŗ
!
š„Ŗ
š
š„
š
48. @joel__lord
#iJS18
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 !
š„Ŗ
š
š„
š
š„Ŗ
!
š„Ŗ
š
š„
š
š„Ŗ
!
š„Ŗ
š
š„
š
š„Ŗ
!
š„Ŗ
š
š„
š
š„Ŗ
49. @joel__lord
#iJS18
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
#iJS18
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
#iJS18
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
#iJS18
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
#iJS18
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
#iJS18
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
#iJS18
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
#iJS18
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
#iJS18
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
#iJS18
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
#iJS18
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
#iJS18
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
#iJS18
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
#iJS18
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
!
š„Ŗ
š
š„
š
š„Ŗ
š
!
š„Ŗ
š„Ŗ
š„
!
š„Ŗ
š
š
š„
š„Ŗ
š
š„Ŗ
63. @joel__lord
#iJS18
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
!
š„Ŗ
š
š„
š
š„Ŗ
š
!
š„Ŗ
š„Ŗ
š„
!
š„Ŗ
š
š
š„
š„Ŗ
š
š„Ŗ
š„Ŗ
64. @joel__lord
#iJS18
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
!
š„Ŗ
š
š„
š
š„Ŗ
š
š„
š
š„Ŗ
š
š„
š
š„Ŗ
!
š„Ŗ
!
š„Ŗ
š„Ŗ
š„Ŗ
š„Ŗ
69. @joel__lord
#iJS18
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");
70. @joel__lord
#iJS18
What is a callback
! Simply a function as
an argument to a
function
! Could be
synchronous or
asynchronous
71. @joel__lord
#iJS18
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
#iJS18
What is a callback
! Simply a function as
an argument to a
function
! Could be
synchronous or
asynchronous
73. @joel__lord
#iJS18
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");
74. @joel__lord
#iJS18
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ā);
}..
.....................
75. @joel__lord
#iJS18
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");
76. @joel__lord
#iJS18
What is a callback
Asynchronous callback and the event loop
fs.readFile("./cb.txt",
(err, data) => {
console.log(data);
});
console.log("after");
77. @joel__lord
#iJS18
What is a callback
Asynchronous callback and the event loop
console.log("after"); (err, data) => {
console.log(data);
}
78. @joel__lord
#iJS18
What is a callback
Asynchronous callback and the event loop
(err, data) => {
console.log(data);
}
79. @joel__lord
#iJS18
What is a callback
Asynchronous callback and the event loop
(err, data) => {
console.log(data);
}
98. @joel__lord
#iJS18
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");
99. @joel__lord
#iJS18
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");
100. @joel__lord
#iJS18
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
#iJS18
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
#iJS18
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
#iJS18
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
#iJS18
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
#iJS18
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
#iJS18
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
#iJS18
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
#iJS18
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
#iJS18
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
#iJS18
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
#iJS18
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
#iJS18
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
#iJS18
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
#iJS18
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
#iJS18
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
#iJS18
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
#iJS18
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
#iJS18
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
#iJS18
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
#iJS18
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
#iJS18
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
#iJS18
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
#iJS18
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);
});