0

Imagine having an expressjs application and having an async tracking method to save in the database certain events:

class Tracking {

    static async track(event) {
       // saves the event in the DB
    }

}

Is there some side effect or, in general, something wrong in doing something like this?

app.get('/say-hello-world', function(req, res) {
  Tracking.track("say-hello-world"); // ignore Promise
  console.log("HW");
  Tracking.track("data-logged"); // ignore Promise
  res.send('hello world');
});

The tracking code will be executed in parallel and performance should benefit from this. But is there something wrong in doing this (and not using await)?

Giordano
  • 1,401
  • 15
  • 26
  • The only side effect is that your `data-logged` track might complete before your `say-hello-world` track. Also your send might occur before your tracks complete (which depending on your situation may be good or not) – kemicofa ghost Oct 28 '19 at 15:51
  • You might want to checkout [Promise#all](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise/all) and [Promise#allSettled](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise/allSettled) – kemicofa ghost Oct 28 '19 at 15:52
  • in general, there's nothing wrong with not `await`ing an `async` function. But if you don't need to `await`, why even bother with `async`? Also, you cannot `await` in a normal function. – marzelin Oct 28 '19 at 15:55
  • Also, response will be sent before db writes are completed. – marzelin Oct 28 '19 at 15:58
  • @kemicofa however this is not a problem as long as the timestamp is created before saving the row in the DB (the async part of the code) – Giordano Oct 28 '19 at 16:11
  • @marzelin my goal is in fact to avoid that the tracking code slows down the controllers. – Giordano Oct 28 '19 at 16:12

0 Answers0