Concurrency VS Parallelism Concurrency Two or more tasks are happening at the same interval Parallelism Two or tasks are happening at the exact same instant (e.g. multicore processors)
Promises ● A future value representation that emits completion events ● Facilitate sequential and parallel flow control ● Chainable through a “thenable” interface ● Allows deferring callback setup
Promise Trust ● Always async, even if already settled ● Encapsulate state ● Either success or fail ● Always resolve or reject only once ● Become immutable once resolved ● Facilitate error handling
Always async? Tasks Triggers such as DOM events and setTimeout enqueue tasks Microtasks Scheduled for things that should happen straight after the currently executing task. Promise callbacks are usually called as microtasks.
Observables ● Lazy event streams which can emit zero or more events, and may or may not finish ● Values can be Promises or other Observables ● EventEmitter API + Iterable API = Pub/Sub model on steroids
Why Generators? ● Handle asynchronicity in a synchronous fashion by yielding promises ● Alleviate the need for callbacks ● Elegant error handling - throw & try/catch wrapping
Thunks ● A function that wraps some behavior for later execution ● Accepts only a callback as an argument ● Enforces the CPS paradigm ● May return another thunk - aka chainable
Why thunks? ● Thunks can be used as yieldable structures for generator based flow controllers as an alternative to Promises ● Co - github.com/tj/co ● Redux-thunk - github.com/gaearon/redux-thunk
CSP ● Coordination via message passing based on channels ● Channels are 2-way streams (default buffer = 1) ● A channel cannot accept a message unless someone on the other side is reading ● JS-CSP: Operations on channels are yieldable from inside generators