NDC: Async and Streaming JavaScript, We're All Doing it Wrong! (Promises, Streams, Rx)
By Matthew Podwysocki
Lot of work (setup, remove listeners ...), not composable.
No way to abort promise in progress. (Me: has to remember to check for errors:
No try-catch-finally; only try-catch.
Node: Stream 1 were terrible (pause/resume unusable, data sent before ready, ...).
Streams 2: A lot to trip over. Streams 3: Similar.
WHATWG Streams:
Behavior: values over time Event: discrete phenomena with a value and a time
"The majority of your asynchronous code is written with just a few flexible functions."
Observables: No need for low-level programming, combine existing powerful primitives (
Futures = promises, streams unify IO and events, mirrors Rx by adding map/... .
See https://github.com/zenparsing/es-observable/blob/master/README.md
Events
Lot of work (setup, remove listeners ...), not composable.
Promises
No way to abort promise in progress. (Me: has to remember to check for errors:
then(onOk, onError)
.)No try-catch-finally; only try-catch.
Streams
Node: Stream 1 were terrible (pause/resume unusable, data sent before ready, ...).
Streams 2: A lot to trip over. Streams 3: Similar.
WHATWG Streams:
- focused on low-level I/I, not on object mode
- Think Node Streams + Promises
Reactive Programming
Behavior: values over time Event: discrete phenomena with a value and a time
"The majority of your asynchronous code is written with just a few flexible functions."
The general theory of reactivity
^
multiple values | iterable observable
single value | value promise
---------------------------------->
sync async
Observables: No need for low-level programming, combine existing powerful primitives (
debounce
, flatMapLatest
, ...).JS Future
Async/Await a la C#?
- for promises
Dart: Async progr. with futures and streams
Futures = promises, streams unify IO and events, mirrors Rx by adding map/... .
Observables coming to ES2016
See https://github.com/zenparsing/es-observable/blob/master/README.md
Resources
- http://jhusain.github.io/learnrx/
- http://rxmarbles.com/ - visual presentation of fns
- http://xgrommx.github.io/rx-book/
- visual debugger for async code with Rx: http://jaredforsyth.com/rxvision/
- RxJS Pacman game ex.: https://github.com/Reactive-Extensions/RxJS/blob/master/examples/pacman-unicode/index.html (and http://www.sitepoint.com/building-pacman-with-bacon-js/)