当前位置: 首页>>编程示例 >>用法及示例精选 >>正文


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)。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。