Before we get into developing our application, we need to take a deeper look at using the Promise class and async functions with Express because Express was invented before these features existed, and so it does not directly integrate with them. While we should be using async functions wherever possible, we have to be aware of how to properly use them in certain circumstances, such as in an Express application.
The rules in Express for handling asynchronous execution are as follows:
- Synchronous errors are caught by Express and cause the application to go to the error handler.
- Asynchronous errors must be reported by calling next(err).
- A successfully executing middleware function tells Express to invoke the next middleware by calling next().
- A router function that returns a result to the HTTP request does not call next().
In this section, we'll discuss three ways to use Promises and async functions in a way that is...