网络请求、远程资源与客户端存储技术详解
1. 网络请求与流处理
在网络请求中,可以利用双流技术对响应流进行检查和操作。由于可以使用 ReadableStream 创建 Response 对象,因此能够读取一个流,将其管道传输到新创建的二级流中,并使用该二级流进行 Body 方法(如 text() )的操作。示例代码如下:
fetch('https://fetch.spec.whatwg.org/')
.then((response) => response.body)
.then((body) => {
const reader = body.getReader();
// create secondary stream
return new ReadableStream({
async start(controller) {
try {
while (true) {
const { value, done } = await reader.read();
if (done) {
break;
}
// Push the body stream's chunk onto the secondary stream
controller.enqueue(value);
超级会员免费看
订阅专栏 解锁全文
7万+

被折叠的 条评论
为什么被折叠?



