最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

Javascript stream text in small chunks - Stack Overflow

programmeradmin2浏览0评论

I'm using a Vue.js app to read text from a server. The server streams small amounts of text with each request. For example, the total request lasts 10 seconds and the server writes 1 word per second to the stream.

I've tried this:

fetch('https://my-streaming-api').then(response => {
const reader = response.body.getReader();

async function readStream() {
  while (true) {
    const { done, value } = await reader.read();
    if (done) break;

    // Process the chunk here (e.g., display it)
    console.log('Chunk:', value); 
  }
}

readStream();
});

The problem I have is that the read() function will block until the whole request is done, and the returned value will be all the data. I'd like to be able to display each word (in this case) as it comes in. I suspect there is some built-in chunk size or internal buffering going on. Is there any way to control this?

发布评论

评论列表(0)

  1. 暂无评论