Concurrently JavaScript (1) | getiblog

In notebook:
Article Notes
Created at:
2015-11-24
Updated:
2015-11-24
Tags:
JavaScript Fundamentals
Concurrently JavaScript (1) | getiblog
In fact, normal JS functions have “run to completion” semantics, meaning that in fact no function that’s currently running can be preemtively interrupted by another function. Of course, function A can call function B. But B can never be scheduled to start running in parallel with A.

That means A never needs to worry about any side effects that B may have on the program state, except from the moment A invokes B to when B fully completes.

This semantic is significantly simplifying in terms of ease of writing correct, bug-free code.
To highest level concepts to manage concurrency:
  • things happening in parallel
  • things happening in series
Javascript concurrency


Callback
express a series, you call one callback at the end of the previous one
Thunks
a pattern for organizing closure in callbacks to eliminate ordering (time) complexity. So it’s much easier to express both series and parallel with them.
Promises
are like thunks but with more trustability and a nicer API. So, concurrency is even better with promises than with thunks. A series is a promise chain, and a parallel group is achieved with Promise.all(..).
Generators (+Promises)
achieve almost perfect sync-looking async code, which makes expressing our coordinated concurrency look even closer to how our brains work.