2

I'm working on improving the response time for one of our API routes.

We make a couple of requests to post messages to a message queue, and currently we await the network request.

const someMiddlewareFunction = async (ctx, next) => {
    ...
    await postThingToQueue(message_payload)
    ...
    ctx.body = {...}
    return next()
}

What would be the downsides of not awaiting the postThingToQueue function if we don't care if the message was sent to the queue? (let's just say we really don't care and the proper error handling was taken care of)

UnbrandedTech
  • 461
  • 6
  • 14

1 Answers1

3

It's totally up to you how you want it to work. If you want the middleware to be done and Express to proceed with other request handlers for this particular request without waiting for the async operation to complete and you have other error handling that will take care of things, then there is no downside. This is sometimes referred to as an asynchronous operation that is "fire and forget". It is not the usual way things are done, but there certainly are cases where it is appropriate.

If you have a proper error handling scheme and there is no need for the request itself to wait for the queuing operation, then this is perfectly fine. No downside. In fact, there can be upside in that the request may be processed faster because it isn't blocked by the queuing operation.

jfriend00
  • 683,504
  • 96
  • 985
  • 979