Backpressure Idea Demo

Backpressure Idea Demo

Hard NodeJS Streams & Buffers 35 views
Explanation Complexity

Problem Statement

Pipe a readable to writable and log when drain happens.

Input Format

No input.

Output Format

Print some logs.

Constraints

Use a slow writable.

Input / Output Format

Input Format
No input.
Output Format
Print some logs.
Constraints
Use a slow writable.

Example Solution (Public)

NodeJS
const { Readable, Writable } = require('stream');

const src = Readable.from(Array.from({ length: 50 }, () => 'meetcode\
'));
const sink = new Writable({
  highWaterMark: 8,
  write(chunk, enc, cb) {
    setTimeout(cb, 5);
  }
});

sink.on('drain', () => console.log('drain'));
src.pipe(sink).on('finish', () => console.log('done'));

Official Solution Code

const { Readable, Writable } = require('stream');

const src = Readable.from(Array.from({ length: 50 }, () => 'meetcode\
'));
const sink = new Writable({
  highWaterMark: 8,
  write(chunk, enc, cb) {
    setTimeout(cb, 5);
  }
});

sink.on('drain', () => console.log('drain'));
src.pipe(sink).on('finish', () => console.log('done'));
Please login to submit solutions.
Editor
Output

                                        
Please login to submit solutions.