用于在微信小程序中实现 http 的流式响应, 无关框架, 在原生小程序
、UniApp
、Taro
等都适用
-
pnpm add chunk-res
-
直接从Github下载
chunkRes.(ts/js)
到项目中即可
// Example:
const chunkRes = ChunkRes();
// can`t use ref() to save task; it will lost task info
const task = wx.request({
//...other params
enableChunked: true,
success: (res) => {
const lastResTexts: string[] | undefined = chunkRes.onComplateReturn();
// dosomething
},
});
task.onChunkReceived((res) => {
const resTexts: string[] | undefined = chunkRes.onChunkReceivedReturn(
res.data
);
// dosomething
});