Fastify: async + reply.send

Created on 18 Sep 2017  路  3Comments  路  Source: fastify/fastify

Hi,
this handle responses always empty body with 200 status code

fastify.get('/', async (req, reply) => {
  reply.send({})
})

Maybe we need to figure out how to report (or throw an exception)
I lost 30minutes last time... 馃槩

bug

Most helpful comment

In this block https://github.com/fastify/fastify/blob/master/lib/reply.js#L29-L31, reply.sent聽is set to true after a setImmediate ends.

When returning a Promise, the promise is passed in https://github.com/fastify/fastify/blob/master/lib/handleRequest.js#L120. In https://github.com/fastify/fastify/blob/master/lib/reply.js#L76-L78 we handle that promise. As you can see in https://github.com/fastify/fastify/blob/master/lib/reply.js#L166, we are just calling reply.send()聽 with the payload.

All of this happens _asynchronously_, and the check in https://github.com/fastify/fastify/blob/master/lib/reply.js#L29-L31 is not triggered.

I will prepare a fix.

All 3 comments

Why it shouldn't?

In JavaScript if not specified every function return undefined at the end of its execution.
Since you are using async/await, this will be triggered and that's why you get an empty body.

I'm wondering why we are not throwing an exception in that case, since you are calling send two times (one with the implicit return and the one you written).

In this block https://github.com/fastify/fastify/blob/master/lib/reply.js#L29-L31, reply.sent聽is set to true after a setImmediate ends.

When returning a Promise, the promise is passed in https://github.com/fastify/fastify/blob/master/lib/handleRequest.js#L120. In https://github.com/fastify/fastify/blob/master/lib/reply.js#L76-L78 we handle that promise. As you can see in https://github.com/fastify/fastify/blob/master/lib/reply.js#L166, we are just calling reply.send()聽 with the payload.

All of this happens _asynchronously_, and the check in https://github.com/fastify/fastify/blob/master/lib/reply.js#L29-L31 is not triggered.

I will prepare a fix.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

jsumners picture jsumners  路  4Comments

sebdeckers picture sebdeckers  路  4Comments

mcollina picture mcollina  路  4Comments

aaronshaf picture aaronshaf  路  3Comments

lean picture lean  路  3Comments