Promises
- Promises are one approach to limiting callback hell.
- Writing code that sets up a promise can be a bit tricky; but
- Writing code that uses a promise that someone else sets up is
almost always much cleaner than callback hell.
- Look at
ExpressMVCSqlitePromises
- Let’s start by looking at the “end user’s” view in
index.js
- When looking at
index, the differences appear minimal.
(The callback is just pushed into a then method).
- The
show method demonstrates how to handle errors.
Notice that there is a catch method instead of an if/else.
- This is a moderate improvement, but not a huge deal. (In my opinion.)
- The big improvement comes when we need to nest/chain promises.
- Look at
dbTest.js
- We want the results of two independant queries (toys below $10 and toys above $100).
- Promises give us an easy way to run these in parallel and combine the results.