Javascript stream text in small chunks - Stack Overflow

I'm using a Vue.js app to read text from a server. The server streams small amounts of text with e

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?

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745352591a4623925.html

相关推荐

  • Javascript stream text in small chunks - Stack Overflow

    I'm using a Vue.js app to read text from a server. The server streams small amounts of text with e

    2小时前
    10

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信