8
app.use(async function(req, res, next) {
    try {
        var myres = await new Promise((resolve, reject) => {
            mysql_connection.query("select * from Users;", (err, rows) => {
                if (err) {
                    reject(err);
                } else {
                    resolve(rows);
                }
            });
        });
    } catch (error) {
        console.log(error);
    }
});

Question is. using async function to be able to use await for DB queries is ok? i am afraid that it can cause some problems on expressjs side.

Estus Flask
  • 206,104
  • 70
  • 425
  • 565
Anton Stafeyev
  • 761
  • 1
  • 7
  • 20
  • 1
    Are you ignoring `myres` on purpose? Otherwise, you can simply call `next` with error, if any (I am assuming you are already aware of this). – thefourtheye Jul 26 '18 at 09:31

4 Answers4

23

async..await is syntactic sugar for promises, and a promise is just a pattern that relies on callbacks. The use of async functions is acceptable wherever they are supported by the environment. async..await is supported since Node.js 7.6.0.

async function always returns a promise. As long as this implicit return value doesn't cause problems, it's perfectly fine everywhere, including Express. It doesn't matter whether it's used for database queries or anything else.

Unless API supports promises, errors should be entirely handled in async function. Function body should be wrapped with try..catch to rule out unhandled rejections which may result in exceptions in future Node versions.

The original code contains no next calls and just suppresses an error. As a rule of thumb, async middleware should be structured like that:

app.use(async function(req, res, next) {
    try {
        ...
        next();
    } catch (error) {
        next(error);
    }
});
Estus Flask
  • 206,104
  • 70
  • 425
  • 565
  • estus why does the code inside catch contain next? as you mentioned `The original code contains no next calls and just suppresses an error.` note: if I don't use next, it still throws unhandle Promise warning. Also, I went through this question: https://stackoverflow.com/questions/13133071/express-next-function-what-is-it-really-for – Alwaysblue Dec 27 '18 at 08:45
  • 1
    Also If you could a minute or two on this question as well? https://stackoverflow.com/questions/53941672/how-to-handle-error-and-use-promises-correctly/53942095#53942095 (will be great help) – Alwaysblue Dec 27 '18 at 08:46
  • 2
    Suppressing an error is rarely a good idea. In case of a middleware that is supposed to send a response (this isn't shown in OP), doing nothing would result in no response and request timeout. `next` delegates the error to Express error handler. It's a common way to handle errors in Express. You can use `res.status(400).send(...)` instead of `next(err)` in case you have more specific idea how to handle this error. *if I don't use next, it still throws unhandle Promise warning.* - you're likely doing something wrong. – Estus Flask Dec 27 '18 at 09:02
8

Async await can be used with no problem for DB queries. You could use try catch however there is a more elegant solution which allows you to use the error handling middleware which express offers:

You wrap your middleware with this function:

const asyncMiddleware = fn =>
  (req, res, next) => {
    Promise.resolve(fn(req, res, next))
      .catch(next);
  };

Then you can use it in the following manner:

const asyncMiddleware = require('./utils/asyncMiddleware');

router.get('/', asyncMiddleware(async (req, res, next) => {
    /* 
      if there is an error thrown in getUserFromDb, asyncMiddleware
      will pass it to next() and express will handle the error;
    */
    const user = await getUserFromDb({ id: req.params.id })
    res.json(user);
}));

If an error is thrown the control will be handed over to the error handling middleware which is middlware which has four arguments like this:

app.use(function (err, req, res, next) {
   // your error code
})
Willem van der Veen
  • 33,665
  • 16
  • 190
  • 155
2

You also can import on your server.ts or server.js the https://www.npmjs.com/package/express-async-errors that handle async errors on express

yarn add express-async-errors

And import it import 'express-async-errors'; on your main file.

1

Using async function to be able to use await for DB queries is totally okay. But try to embed that code in try and catch block so you can catch the exceptions.

Zeeshan Tariq
  • 604
  • 5
  • 10
  • But ne thing which left unanswered, it will not cause some unpredictable behavior on express side ? – Anton Stafeyev Jul 26 '18 at 09:40
  • It won't . Please have a look on this blog: https://medium.com/@Abazhenov/using-async-await-in-express-with-node-8-b8af872c0016 I hope it helps. – Zeeshan Tariq Jul 26 '18 at 09:49