當前位置: 首頁>>編程示例 >>用法及示例精選 >>正文


Node.js stream.Duplex.toWeb(streamDuplex)用法及代碼示例

stream.Duplex.toWeb(streamDuplex)

添加於:v17.0.0
Stability: 1 - 實驗性

參數
import { Duplex } from 'node:stream';

const duplex = Duplex({
  objectMode: true,
  read() {
    this.push('world');
    this.push(null);
  },
  write(chunk, encoding, callback) {
    console.log('writable', chunk);
    callback();
  }
});

const { readable, writable } = Duplex.toWeb(duplex);
writable.getWriter().write('hello');

const { value } = await readable.getReader().read();
console.log('readable', value);const { Duplex } = require('node:stream');

const duplex = Duplex({
  objectMode: true,
  read() {
    this.push('world');
    this.push(null);
  },
  write(chunk, encoding, callback) {
    console.log('writable', chunk);
    callback();
  }
});

const { readable, writable } = Duplex.toWeb(duplex);
writable.getWriter().write('hello');

readable.getReader().read().then((result) => {
  console.log('readable', result.value);
});

相關用法


注:本文由純淨天空篩選整理自nodejs.org大神的英文原創作品 stream.Duplex.toWeb(streamDuplex)。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。