From 003eae651861ffff9f8a8910039a4577175d6979 Mon Sep 17 00:00:00 2001 From: Tikkhun Date: Wed, 28 Dec 2022 05:36:28 +0800 Subject: [PATCH] docs(reference/reply): When using async-await, need return (#4429) * docs(reference/reply): When using async-await, please return the reply object to signal fastify wait for your further response. * docs(reference/reply): When using async-await, please return the reply object to signal fastify wait for your further response. * Update docs/Reference/Reply.md Co-authored-by: Frazer Smith * Update docs/Reference/Reply.md Co-authored-by: Frazer Smith * Apply suggestions from code review Co-authored-by: Andrey Chalkin * docs: return or await reply when async-await Co-authored-by: Frazer Smith Co-authored-by: Andrey Chalkin --- docs/Reference/Reply.md | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/docs/Reference/Reply.md b/docs/Reference/Reply.md index b5d690ae83..0f8e99db79 100644 --- a/docs/Reference/Reply.md +++ b/docs/Reference/Reply.md @@ -674,6 +674,15 @@ fastify.get('/streams', function (request, reply) { reply.send(stream) }) ``` +When using async-await you will need to return or await the reply object: +```js +fastify.get('/streams', async function (request, reply) { + const fs = require('fs') + const stream = fs.createReadStream('some-file', 'utf8') + reply.header('Content-Type', 'application/octet-stream') + return reply.send(stream) +}) +``` #### Buffers @@ -689,6 +698,16 @@ fastify.get('/streams', function (request, reply) { }) ``` +When using async-await you will need to return or await the reply object: +```js +const fs = require('fs') +fastify.get('/streams', async function (request, reply) { + fs.readFile('some-file', (err, fileBuffer) => { + reply.send(err || fileBuffer) + }) + return reply +}) +``` #### Errors