TransformStream: thuộc tính readable

Baseline Widely available

This feature is well established and works across many devices and browser versions. It’s been available across browsers since June 2022.

Note: This feature is available in Web Workers.

Thuộc tính chỉ đọc readable của giao diện TransformStream trả về phiên bản ReadableStream được điều khiển bởi TransformStream này. Luồng này phát ra dữ liệu đầu ra được chuyển đổi.

Giá trị

Một ReadableStream.

Ví dụ

Ví dụ này tạo TransformStream chuyển đổi tất cả văn bản đầu vào thành chữ in hoa. Nó ghi một số văn bản vào luồng writable, sau đó đọc văn bản đã chuyển đổi từ luồng readable.

js
const stream = new TransformStream({
  transform(chunk, controller) {
    controller.enqueue(chunk.toUpperCase());
  },
});

// Write data to be transformed
const writer = stream.writable.getWriter();
writer.write("hello ");
writer.write("world");
writer.close();

// Read transformed data
const reader = stream.readable.getReader();
let done = false;
let output = "";
while (!done) {
  const result = await reader.read();
  if (result.value) {
    output += result.value;
  }
  done = result.done;
}
console.log(output); // HELLO WORLD

Thông số kỹ thuật

Specification
Streams
# ref-for-ts-readable②

Khả năng tương thích của trình duyệt