DEV Community

Discussion on: Writing Async/Await Middleware in Express

josser profile image
Dmitry Chirkin

There is one thing in your code which may break: (sorry if my english is not good)

Take a look at this code:

function someethingAsync() {
  return new Promise((resolve, reject) => {
    setTimeout(() => { reject() }, 1000);

async function test() {
  const data = await someethingAsync();
  console.log('Will you see me?')


If you run it, you will never see console.log's
And the same is true for you middleware code
If PromiseBasedDataRequest will be rejected then you never reach next() and execution will hang.

Rule of thumb here is simple: when we trying to connect Promise-style and callback-style code and you are somewhere in callback, you should always write .catch() block.