fastify/avvio

async function as onClose handler

gyszalai opened this issue · 2 comments

If I register an asynchronous onClose handler as a two argument (context, done) function:

async function myPlugin (instance, opts) {
  instance.onClose((context, done) => {
    setTimeout(done, 3000)
  })
}

when app.close() is called, the next onClose handler does not start until the done() callback is not called on the previous one.

But if I register an asynchronous onClose handler as an async function:

async function myPlugin (instance, opts) {
  instance.onClose(async () => {
    await new Promise(resolve => setTimeout(resolve, 3000))
  })
}

when app.close() is called, the next onClose handler does not wait until the async function finishes, i.e. the promise resolves.

Is it intentional? It would be much easier to write asynchronous onClose handlers with async functions.

We are probably lacking support for async functions in onClose handlers. Would you like to send a PR?

hm... I'm trying to understand how it works.