![Page 1: Harnessing asynchronicity callbacks, promises, generators, and coroutines](https://reader031.vdocuments.mx/reader031/viewer/2022020217/554a112eb4c90507558b4cf5/html5/thumbnails/1.jpg)
Hao-kang Den <github @hden>
Harnessing asynchronicity - callbacks, promises, generators, and coroutines
![Page 2: Harnessing asynchronicity callbacks, promises, generators, and coroutines](https://reader031.vdocuments.mx/reader031/viewer/2022020217/554a112eb4c90507558b4cf5/html5/thumbnails/2.jpg)
![Page 3: Harnessing asynchronicity callbacks, promises, generators, and coroutines](https://reader031.vdocuments.mx/reader031/viewer/2022020217/554a112eb4c90507558b4cf5/html5/thumbnails/3.jpg)
Agenda
● Why asynchronous?
● Callback
● Promise
● Generator [--harmony]
● Coroutines [--harmony]
![Page 4: Harnessing asynchronicity callbacks, promises, generators, and coroutines](https://reader031.vdocuments.mx/reader031/viewer/2022020217/554a112eb4c90507558b4cf5/html5/thumbnails/4.jpg)
Why asynchronous?Callbacks
![Page 5: Harnessing asynchronicity callbacks, promises, generators, and coroutines](https://reader031.vdocuments.mx/reader031/viewer/2022020217/554a112eb4c90507558b4cf5/html5/thumbnails/5.jpg)
Original slides introducing node.js by Ryan Dahl @jsconf 2009
@ry
![Page 6: Harnessing asynchronicity callbacks, promises, generators, and coroutines](https://reader031.vdocuments.mx/reader031/viewer/2022020217/554a112eb4c90507558b4cf5/html5/thumbnails/6.jpg)
@ry
![Page 7: Harnessing asynchronicity callbacks, promises, generators, and coroutines](https://reader031.vdocuments.mx/reader031/viewer/2022020217/554a112eb4c90507558b4cf5/html5/thumbnails/7.jpg)
@ry
![Page 8: Harnessing asynchronicity callbacks, promises, generators, and coroutines](https://reader031.vdocuments.mx/reader031/viewer/2022020217/554a112eb4c90507558b4cf5/html5/thumbnails/8.jpg)
@ry
![Page 9: Harnessing asynchronicity callbacks, promises, generators, and coroutines](https://reader031.vdocuments.mx/reader031/viewer/2022020217/554a112eb4c90507558b4cf5/html5/thumbnails/9.jpg)
@ry
![Page 10: Harnessing asynchronicity callbacks, promises, generators, and coroutines](https://reader031.vdocuments.mx/reader031/viewer/2022020217/554a112eb4c90507558b4cf5/html5/thumbnails/10.jpg)
@ry
![Page 11: Harnessing asynchronicity callbacks, promises, generators, and coroutines](https://reader031.vdocuments.mx/reader031/viewer/2022020217/554a112eb4c90507558b4cf5/html5/thumbnails/11.jpg)
@ry
![Page 12: Harnessing asynchronicity callbacks, promises, generators, and coroutines](https://reader031.vdocuments.mx/reader031/viewer/2022020217/554a112eb4c90507558b4cf5/html5/thumbnails/12.jpg)
@ry
![Page 13: Harnessing asynchronicity callbacks, promises, generators, and coroutines](https://reader031.vdocuments.mx/reader031/viewer/2022020217/554a112eb4c90507558b4cf5/html5/thumbnails/13.jpg)
@ry
![Page 14: Harnessing asynchronicity callbacks, promises, generators, and coroutines](https://reader031.vdocuments.mx/reader031/viewer/2022020217/554a112eb4c90507558b4cf5/html5/thumbnails/14.jpg)
@ry
![Page 15: Harnessing asynchronicity callbacks, promises, generators, and coroutines](https://reader031.vdocuments.mx/reader031/viewer/2022020217/554a112eb4c90507558b4cf5/html5/thumbnails/15.jpg)
@ry
![Page 16: Harnessing asynchronicity callbacks, promises, generators, and coroutines](https://reader031.vdocuments.mx/reader031/viewer/2022020217/554a112eb4c90507558b4cf5/html5/thumbnails/16.jpg)
@ry
![Page 17: Harnessing asynchronicity callbacks, promises, generators, and coroutines](https://reader031.vdocuments.mx/reader031/viewer/2022020217/554a112eb4c90507558b4cf5/html5/thumbnails/17.jpg)
@ry
![Page 18: Harnessing asynchronicity callbacks, promises, generators, and coroutines](https://reader031.vdocuments.mx/reader031/viewer/2022020217/554a112eb4c90507558b4cf5/html5/thumbnails/18.jpg)
@ry
![Page 19: Harnessing asynchronicity callbacks, promises, generators, and coroutines](https://reader031.vdocuments.mx/reader031/viewer/2022020217/554a112eb4c90507558b4cf5/html5/thumbnails/19.jpg)
Pyramid of Doomstep1(function (value1) {
step2(value1, function(value2) {
step3(value2, function(value3) {
step4(value3, function(value4) {
// Do something with value4
});
});
});
});
Hairball
![Page 20: Harnessing asynchronicity callbacks, promises, generators, and coroutines](https://reader031.vdocuments.mx/reader031/viewer/2022020217/554a112eb4c90507558b4cf5/html5/thumbnails/20.jpg)
Promise/A+Q.fcall(promisedStep1)
.then(promisedStep2)
.then(promisedStep3)
.then(promisedStep4)
.then(function (value4) {
// Do something with value4
})
.catch(function (error) {
// Handle any error from all above steps
})
.done(); https://github.com/kriskowal/q
![Page 21: Harnessing asynchronicity callbacks, promises, generators, and coroutines](https://reader031.vdocuments.mx/reader031/viewer/2022020217/554a112eb4c90507558b4cf5/html5/thumbnails/21.jpg)
ECMAScript a.k.a. JavaScript
● version 1 - June 1997● version 2 - June 1998● version 3 - December 1999● version 4 - Abandoned● version 5 - December 2009● version 5.1 - June 2011● version 6 - [Harmony] Work in progress
![Page 22: Harnessing asynchronicity callbacks, promises, generators, and coroutines](https://reader031.vdocuments.mx/reader031/viewer/2022020217/554a112eb4c90507558b4cf5/html5/thumbnails/22.jpg)
Generatorsfunction* fibonacci() {
let [prev, curr] = [0, 1];
for (;;) {
[prev, curr] = [curr, prev + curr];
yield curr;
}
}
let seq = fibonacci();
print(seq.next()); // 1
print(seq.next()); // 2
print(seq.next()); // 3
print(seq.next()); // 5
print(seq.next()); // 8
ECMAScript 6 a.k.a Harmony
![Page 23: Harnessing asynchronicity callbacks, promises, generators, and coroutines](https://reader031.vdocuments.mx/reader031/viewer/2022020217/554a112eb4c90507558b4cf5/html5/thumbnails/23.jpg)
Coroutinesvar fs = require(‘fs’);
function read(path) {
return function(done){
fs.readFile(path, ‘utf8', done);
}
}
makeCoroutine(function *(){
var a = yield read(‘Readme.md’);
var b = yield read(‘package.json’);
console.log(a);
console.log(b);
});
generator
![Page 24: Harnessing asynchronicity callbacks, promises, generators, and coroutines](https://reader031.vdocuments.mx/reader031/viewer/2022020217/554a112eb4c90507558b4cf5/html5/thumbnails/24.jpg)
Koa.jsExpressive middleware for node.js using generators-- by the core team of expres
app.use(function *(next){ var start = new Date; yield next; var ms = new Date - start; this.set('X-Response-Time', ms + 'ms');});
![Page 25: Harnessing asynchronicity callbacks, promises, generators, and coroutines](https://reader031.vdocuments.mx/reader031/viewer/2022020217/554a112eb4c90507558b4cf5/html5/thumbnails/25.jpg)
generator
![Page 26: Harnessing asynchronicity callbacks, promises, generators, and coroutines](https://reader031.vdocuments.mx/reader031/viewer/2022020217/554a112eb4c90507558b4cf5/html5/thumbnails/26.jpg)
Further Reading● http://latentflip.com/imperative-vs-declarative/● https://medium.com/code-adventures/174f1fe66127● https://github.com/kriskowal/q● http://taoofcode.net/promise-anti-patterns/● http://adit.io/posts/2013-04-17-functors,_applicatives,_and_monads_in_pictures.html
read!