-
-
Notifications
You must be signed in to change notification settings - Fork 2.6k
Description
🐛 Bug Report
repl.send inside an async function has created a lot of headache.
- async + reply.send #238
- async + reply.send #238
- Question about async handlers that call reply.send() #548
- Using "reply.send()" in async error handler throws "Reply already sent" error #1131
with fastify/fastify-static#110 we have another issue. The issue ist that an error is logged because sendFile doesn't accept a callback or return a promise. The route is fulfilled to undefined before the file was sent. This behaviour doesn't agree with https://github.com/fastify/fastify/blob/master/docs/Routes.md#promise-resolution.
Due to the current implementation every decorator/plugin which calls .send under the hood must provide a callback or promise to the consumer. If they don't the handler can't use async/await and will loose the control over the response.
Expected behavior
In the longer term we change the api.
// the data flow isnt determined by the function return value (koajs be like)
fastify
.get('/', async (request, reply) => {
reply.body = await data()
repl.body = sync()
});In short term:
Teach and update the docs that you can use only reply.send in async/await if you still return a promise that controls the asynchronous operation.
until send is called
fastify
.get('/', async (request, reply) => {
return op().then(reply.send)
});or we recommend to not use rely.send at all in async functions