- Khalid Jebbari
- Paris.js #28
- March 27th, 2013
The problem (1/2)
Code writing (in editor) and code execution (in time) have a one-one relation in synchronous (blocking) languages.
The coder expects its code to be executed in the same order it's written.
No longer true in asynchronous code.
- High-order function:
- Function that takes anothter function as a parameter.
- Callback :
- Function that is passed to another function.
A reference to executable code, or a piece of executable code, that is passed as an argument to other code. Wikipedia
- Asynchronous callback :
- Callback that will be executed later.
Deferred callbacks are often used in the context of I/O operations or event handling. Wikipedia
The classical async example
The callbacks pyramid
The problem (2/2)
- Prisoner of the callbacks
- Code flow derived from 1:1 between code/execution
- Human is used to read code in one plan
async.js to the rescue
The toolbox to manage any asynchronous code flow
Equivalent of Array.forEach with asynchronous iterations.
Equivalent of Array.map with asynchronous transformations
Other async.js helpers
- filter, reduce and others from ECMA 5
- eachSeries, mapSeries etc. to guarantee order of execution
- eachLimit, mapLimit, etc. to limit # of parallel iterators
- series, parallel, waterfall, queue, compose, memoize, ...
Check the API, you will find something for your use case
What's async.auto ?
A simple way to describe an asynchronous dependency graph.
- Helps writing an asynchronous multi-steps algorithm
- Helps managing dependencies between those steps
- Manages the order of steps
- Gives you the results of all steps at the end
async.auto in action
async.auto's pro tip®