Handling asynchronous: yesterday, today, tomorrow, and after
ParisJS #44
CALLBACKS1
3
We get the data after 1000ms
4
What if I need to chain callbacks?
5
Wtf…
6
PROMISES2
8
ES6 Promises, A+ implementation
9
ES6 Promises, how to use them
10
Back to our example: we get the data after 1000ms
11
Getting better! But still not perfect
12
Should we start using ES6 Promises?
Yes with a polyfill (eg: Babel)
GENERATORS3
14
The function* declaration defines a generator function
Which returns a Generator object
15
The body is not executed. When asked to,
the code is executed until the first ‘yield’ expression
16
How to use a generator function by calling
generator.next()
17
Keep calling .next() till `done` is true
18
Back to our example
19
Wow! Looks great!
Does it work? No
20
Wrapped inside a custom `async` function, it works
Asynchronous code like if it was synchronous
21
The async function is trivial to implement
Or you can use the Q library (Q.spawn)
Or the co library
22
Looking great! Want better?
23
Should we use it yet?
Why not.
- Custom async implementation
- ES6 to ES5 with BabelJS
- BabelJS polyfill for Promises
ES7
ASYNC / AWAIT4
25
ES7 native async / await
`getData` returns a Promise
26
Now that’s awesome
Asynchronous code like if it was synchronous
27
Should we use it yet?
No.
Or just for fun,
it’s too early and things are still moving.
(BabelJS in experimental mode)
SUMMARY5
29
Yesterday: callbacks
30
Today: Promises
31
Tomorrow: Generators?
32
The day after tomorrow: ES7 async / await
THANK YOU
@gmajoulet

Handling asynchronous with JavaScript - ParisJS #44