fetch方法请求并处理接口返回的stream流数据

const fetchStream = async (url, options) => {
  const response = await fetch(url, options);
  if (response.status !== 200) {
    return false;
  }
  const reader = response.body.getReader();
  return reader;

  while (true) {
    const { value, done } = await reader.read();
    if (done) {
      break;
    } else {
      console.log(new TextDecoder().decode(value));
    }
  }
};

fetchRequest("/api/chat/scend", {
  method: "post",
  body: JSON.stringify(data),
})
posted @ 2023-08-21 17:51  Li_pk  阅读(449)  评论(0编辑  收藏  举报