Skip to content

Async style with reply.send #1864

@StarpTech

Description

@StarpTech

🐛 Bug Report

repl.send inside an async function has created a lot of headache.

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    discussionIssues or PRs with this label will never staledocumentationImprovements or additions to documentation

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions