From 23f283f0607449efcef7125e584f91f335648fbd Mon Sep 17 00:00:00 2001 From: thucpn Date: Mon, 25 Nov 2024 15:25:06 +0700 Subject: [PATCH] refactor: use await --- .../express/src/controllers/chat.controller.ts | 12 +++++------- 1 file changed, 5 insertions(+), 7 deletions(-) diff --git a/templates/types/streaming/express/src/controllers/chat.controller.ts b/templates/types/streaming/express/src/controllers/chat.controller.ts index 293bf974..80dac35e 100644 --- a/templates/types/streaming/express/src/controllers/chat.controller.ts +++ b/templates/types/streaming/express/src/controllers/chat.controller.ts @@ -63,19 +63,17 @@ export const chat = async (req: Request, res: Response) => { data: vercelStreamData, callbacks: { onCompletion }, }); - // TODO: move to LlamaIndexAdapter - const reader = streamResponse.body?.getReader(); - function read() { - reader?.read().then(({ done, value }: { done: boolean; value?: any }) => { + if (streamResponse.body) { + const reader = streamResponse.body.getReader(); + while (true) { + const { done, value } = await reader.read(); if (done) { res.end(); return; } res.write(value); - read(); - }); + } } - read(); } catch (error) { console.error("[LlamaIndex]", error); return res.status(500).json({