0
0
Node.jsframework~10 mins

Stream backpressure concept in Node.js - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to create a readable stream from a file.

Node.js
const fs = require('fs');
const readable = fs.createReadStream('[1]');
Drag options to blanks, or click blank then click option'
A'input.txt'
B'output.txt'
C'data.json'
D'log.txt'
Attempts:
3 left
💡 Hint
Common Mistakes
Using an output file name instead of input file name.
Forgetting to put quotes around the file name.
2fill in blank
medium

Complete the code to pause the readable stream when the buffer is full.

Node.js
readable.on('data', (chunk) => {
  if (!writable.write(chunk)) {
    readable.[1]();
  }
});
Drag options to blanks, or click blank then click option'
Apause
Bstop
Chalt
Dend
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'stop' or 'end' which are not valid readable stream methods.
Using 'halt' which is not a method on streams.
3fill in blank
hard

Fix the error in the code to resume the readable stream when the writable stream drains.

Node.js
writable.on('drain', () => {
  readable.[1]();
});
Drag options to blanks, or click blank then click option'
Apause
Bstop
Cresume
Dend
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'pause' again instead of 'resume'.
Using 'stop' or 'end' which do not resume streams.
4fill in blank
hard

Fill both blanks to correctly handle backpressure in a pipe.

Node.js
readable.on('data', (chunk) => {
  if (!writable.[1](chunk)) {
    readable.[2]();
  }
});
Drag options to blanks, or click blank then click option'
Awrite
Bpause
Cend
Ddestroy
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'end' or 'destroy' instead of 'write' for sending data.
Using 'end' or 'destroy' instead of 'pause' to stop reading.
5fill in blank
hard

Fill all three blanks to create a backpressure-aware stream pipe.

Node.js
readable.on('data', (chunk) => {
  if (!writable.[1](chunk)) {
    readable.[2]();
  }
});
writable.on('[3]', () => {
  readable.resume();
});
Drag options to blanks, or click blank then click option'
Awrite
Bpause
Cdrain
Dend
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'end' event instead of 'drain' for writable readiness.
Using 'stop' or 'destroy' methods which do not exist on streams.