Hao-kang Den <github @hden>
Harnessing asynchronicity - callbacks, promises,
generators, and coroutines
Agenda
● Why asynchronous?
● Callback
● Promise
● Generator [--harmony]
● Coroutines [--harmony]
Why asynchronous?
Callbacks
Original slides introducing node.js by
Ryan Dahl @jsconf 2009
@ry
@ry
@ry
@ry
@ry
@ry
@ry
@ry
@ry
@ry
@ry
@ry
@ry
@ry
Pyramid of Doom
step1(function (value1) {
step2(value1, function(value2) {
step3(value2, function(value3) {
step4(value3, ...
Promise/A+
Q.fcall(promisedStep1)
.then(promisedStep2)
.then(promisedStep3)
.then(promisedStep4)
.then(function (value4) {...
ECMAScript a.k.a. JavaScript
● version 1 - June 1997
● version 2 - June 1998
● version 3 - December 1999
● version 4 - Aba...
Generators
function* fibonacci() {
let [prev, curr] = [0, 1];
for (;;) {
[prev, curr] = [curr, prev + curr];
yield curr;
}...
Coroutines
var fs = require(‘fs’);
function read(path) {
return function(done){
fs.readFile(path, ‘utf8', done);
}
}
makeC...
Koa.js
Expressive middleware for
node.js using generators
-- by the core team of expres
app.use(function *(next){
var star...
generator
Further Reading
● http://latentflip.com/imperative-vs-declarative/
● https://medium.com/code-adventures/174f1fe66127
● htt...
Harnessing asynchronicity   callbacks, promises, generators, and coroutines
Upcoming SlideShare
Loading in …5
×

Harnessing asynchronicity callbacks, promises, generators, and coroutines

700 views

Published on

Why & how to manage asynchronous functions.

0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total views
700
On SlideShare
0
From Embeds
0
Number of Embeds
9
Actions
Shares
0
Downloads
1
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

Harnessing asynchronicity callbacks, promises, generators, and coroutines

  1. 1. Hao-kang Den <github @hden> Harnessing asynchronicity - callbacks, promises, generators, and coroutines
  2. 2. Agenda ● Why asynchronous? ● Callback ● Promise ● Generator [--harmony] ● Coroutines [--harmony]
  3. 3. Why asynchronous? Callbacks
  4. 4. Original slides introducing node.js by Ryan Dahl @jsconf 2009 @ry
  5. 5. @ry
  6. 6. @ry
  7. 7. @ry
  8. 8. @ry
  9. 9. @ry
  10. 10. @ry
  11. 11. @ry
  12. 12. @ry
  13. 13. @ry
  14. 14. @ry
  15. 15. @ry
  16. 16. @ry
  17. 17. @ry
  18. 18. Pyramid of Doom step1(function (value1) { step2(value1, function(value2) { step3(value2, function(value3) { step4(value3, function(value4) { // Do something with value4 }); }); }); }); Hairball
  19. 19. 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
  20. 20. 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
  21. 21. Generators function* 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
  22. 22. Coroutines var 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
  23. 23. Koa.js Expressive 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'); });
  24. 24. generator
  25. 25. 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!

×