首页 > 编程语言 >[Javascript] Refactor blocking style code to stream style for fetching the stream data

[Javascript] Refactor blocking style code to stream style for fetching the stream data

时间:2024-08-25 15:48:36浏览次数:6  
标签:body style code const stream resp data

When you use ChatGPT, the response comes in stream, so that it can appears on screen whenever data come back from server, we don't need to wait all data completed then showing the data to users.

 

Here is code which need to be improved, because this code blocking the thread and wait all the data comes back when showing to the screen.

const url = "http://localhost:3000/chat";

async function getResponse(content) {
  const resp = await fetch(url, {
    method: "POST",
    body: JSON.stringify({ content }),
    headers: {
      "Content-Type": "application/json",
    },
  });
  const data = await resp.text();
  console.log(data);
}

 

First we need to understand where the blocking happens?

const data = await resp.text();

 

Then how to resolve this issue? 

We need to convert to stream style, when you check resp.body, you can see the type of it is Body.body: ReadableStream<Uint8Array>

const url = "http://localhost:3000/chat";

async function getResponse(content) {
  const resp = await fetch(url, {
    method: "POST",
    body: JSON.stringify({ content }),
    headers: {
      "Content-Type": "application/json",
    },
  });

  const reader = resp.body.getReader();
  const decoder = new TextDecoder();
  while (1) {
    const { done, value } = await reader.read();
    const text = decoder.decode(value);
    console.log(text);
    if (done) {
      break;
    }
  }
}

 

标签:body,style,code,const,stream,resp,data
From: https://www.cnblogs.com/Answer1215/p/18379022

相关文章

  • AtCoder Beginner Contest 368(ABC368)
    [ABC368F]DividingGame题意:有\(n\)堆石子,第\(i\)堆有\(a_i\)颗石子,每次可以拿走任意一堆石子数量任何数量的棋子,但是要保证拿走之后该堆的石子数量为原来的约数(不能不拿)。问是先手必胜还是后手必胜。\(n,a_i\le10^5\)。思路:发现与Nim游戏类似,且全局信息公开,状态......
  • 嵌入式UI开发-lvgl+wsl2+vscode系列:12、GUI Guider安装使用及在ssd202开发板上测试
    一、前言接下来我们根据开发板官方的指南安装lvgl的ui工具GUIGuider进行开发和测试。理论上还有SquareLineStudio,但是由于一些收费等因素暂时不做过多介绍,gui工具只是辅助,加快开发效率,很多时候还是得直接用代码写界面。(还有一个原因就是GUIGuider可以直接设置中文界面)......
  • Hitachi Vantara Programming Contest 2024(AtCoder Beginner Contest 368)- C
    题意概述有\(N\)个数,分别为\(H_1,H_2,H_3……H_N\)。你将使用初始化为\(0\)的变量\(T\)重复以下操作,直到所有数的值都变为\(0\)或更少。将\(T\)增加\(1\)。然后,减少最前方\(H_i\)值大于等于\(1\)的值。如果\(T\)是\(3\)的倍数,\(H_i\)的值会减少\(3\);......
  • ATcoder368D题详解
    D题传送门一道很无脑的题,但考试没写出来爆搜首先看朴素算法1.从根节点开始遍历每个节点2.遇到要保存的节点就进行标记,直到所有保存节点都标记时间复杂度\(O(n)\)其实已经能过了,但我没用(doge)树链剖分(LCA)首先分析1.每一次砍掉枝叶,都是在没有要保存的节点存在子树上时2.......
  • Leetcode面试经典150题-72.编辑距离
    解法都在代码里,不懂就留言或者私信动态规划最经典题之一,如果写不出来,动态规划好好再学学classSolution{/**这个题是动态规划最经典的题,另一个最经典的是背包问题*/publicintminDistance(Stringword1,Stringword2){/**如果一个为0,取另外一个的长......
  • AtCoder Beginner Contest 368
    A-Cut(abc368A)题目大意给定一个数组,将右边\(k\)个数保持原顺序挪到左边,输出。解题思路即从左边第\(n-k\)个数开始输出即可。或者用rotate函数转换一下。神奇的代码#include<bits/stdc++.h>usingnamespacestd;usingLL=longlong;intmain(void){ios::s......
  • Ros2 vscode 调试运行
    1.编译调试版本:在使用colconbuild编译工作区时,你需要确保以调试模式编译,添加--cmake-args-DCMAKE_BUILD_TYPE=Debug参数:colconbuild--cmake-args-DCMAKE_BUILD_TYPE=Debug2.使用gdb进行调试gdb是一个常用的调试工具,可以帮助你在命令行中调试ROS2节点。步......
  • AtCoder Beginner Contest 368 补题记录(A~D,F,G)
    被伟大的G创似辣。Asignedmain(){intn,k;cin>>n>>k;F(i,1,n)cin>>a[i];queue<int>stk;G(i,n,1)stk.push(a[i]);while(k--){intt=stk.front();stk.push(t);stk.pop();}stack<int>......
  • 线性dp:LeetCode674. 最长连续递增序列
    LeetCode674.最长连续递增序列阅读本文之前,需要先了解“动态规划方法论”,这在我的文章以前有讲过链接:动态规划方法论本文之前也讲过一篇文章:最长递增子序列,这道题,阅读本文的同时可以与“最长递增子序列进行对比”,这样更能对比二者的区别!LeetCode300.最长递增子序列-To......
  • AtCoder Beginner Contest 049
    A-UOIAUAI#include<bits/stdc++.h>usingnamespacestd;usingi64=longlong;intmain(){ ios::sync_with_stdio(false),cin.tie(nullptr); charc; cin>>c; if(c=='a'||c=='e'||c=='i'||c==�......