const resp = await fetch(baseUrl,{
method: 'POST',
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify({})
}
const reader = resp.body.getReader()
const decoder = new TextDecoder() //解码器
//循环拿到所有数据 done读取完毕
while(true){
const {done ,value} = await reader.read()
if(done){
break;
}
const str = decoder.decode(value)
console.log(str) //每次读取的数据,自行处理
}
【笔记】使用fetch进行流式数据接收,仿chatGPT智能对话,文字依次输入
于 2024-01-22 11:03:02 首次发布