rookLab / react-component-caching

Speedier server-side rendering with component caching in React 16
388 stars 26 forks source link

Use express-async-handler in example #54

Closed jordanbtucker closed 5 years ago

jordanbtucker commented 5 years ago

Express does not support async middlware. This PR uses express-async-handler to wrap the the async middleware in the example in the README to avoid confusion.

If you use unwrapped async middleware in express and an error occurs in the middleware, the request will hang and an UnhandledPromiseRejectionWarning will fire.

app.get('/async-error', async (req, res) => {
  throw new Error(`I'm going to wreak havok`)
})

However, if you wrap the async middleware with express-async-handler, errors are handled as expected.

const asyncHandler = require('express-async-handler')

app.get('/wrapped-async-error', asyncHandler(async (req, res) => {
    throw new Error(`I'll be handled just like an error in sync context`)
}))

Note that koa supports async middleware natively.