- What: a syntax for working with async JavaScript functions
- Why: code readability/maintainability
- mitigating callback hell
- getting return values from async functions
- useful when working with multiple async functions
- It isn't necessarily choosing one or the other - often the decision has already been made by the library you're using, such as Knex
- Metaphor: doing favours for someone, such as borrowing a book
- Two aspects: creating and consuming
- Important syntax points:
- resolve == then and reject == catch
- data flows from the return (or resolution) of one function to the parameters of the next function
- Read the contents of one file
- Append the contents into a second file
- List the contents of the second file
- Run the script a few more times to show the increasing output
1-no-functions.js
: an implementation of the demo without using any named functions. This is an illustration of callback hell.2-better-callbacks.js
: an implementation that uses named functions to mitigate callback hell.3-promises.js
: an implementation that uses Promises.4-async-await.js
: an implementation that uses theasync
/await
syntax.