umijs / umi-request

A request tool based on fetch.
2.21k stars 336 forks source link

stream类型支持吗 #320

Open qifenggang opened 1 year ago

qifenggang commented 1 year ago

有个场景要用到stream类型,看了下文档好像没看到相关介绍

puxiao commented 7 months ago

不要过分依赖于 umi-request 或者 ky 这样第三方封装好的请求库,直接用原生的也没多复杂。

const xxxRequest = async () => {

    const response = await fetch(`xxxxx`, {
        method: 'POST',
        headers: { ... },
        body: { ... }
    })

    if (response.body === null) {
        console.error(`网络异常,请求失败`)
        return
    }

    const reader = response.body.pipeThrough(new TextDecoderStream()).getReader()

    while (true) {
        const { done, value } = await reader.read()
        if (done) {
            break
        }
        console.log('新收到数据:', value);
    }

}