All quizzesHard
Streams & Performance — Series 2
Preview — 3 of 10 questions
What is eventually logged?
javascript
const { Transform } = require('stream');
class UppercaseTransform extends Transform {
_transform(chunk, encoding, callback) {
this.push(chunk.toString().toUpperCase());
callback();
}
}
const upper = new UppercaseTransform();
let output = '';
upper.on('data', (chunk) => {
output += chunk;
});
upper.on('end', () => {
console.log(output);
});
upper.write('hello ');
upper.write('world');
upper.end();A"hello world"
B"HELLO WORLD"
C"HELLO " then "WORLD" logged as two separate lines
DTypeError: _transform is not a valid stream method
What does a false return value from writable.write(chunk) signal, and what should well-behaved producer code do in response?
AIt signals the write permanently failed and the chunk was discarded; the same chunk should never be retried.
BIt signals the stream has been closed; no further writes are possible under any circumstances.
Cwrite() never returns false for Writable streams; only Readable streams can signal backpressure.
DIt signals "backpressure" — the stream's internal buffer has reached or exceeded its highWaterMark, meaning the consumer can't keep up; well-behaved code should pause writing further data and wait for the stream's 'drain' event before writing again, to avoid unbounded memory growth.
What is the logging order?
javascript
console.log('start');
setImmediate(() => console.log('setImmediate'));
process.nextTick(() => console.log('nextTick'));
Promise.resolve().then(() => console.log('promise'));
console.log('end');A"start", "end", "promise", "nextTick", "setImmediate"
B"start", "end", "setImmediate", "nextTick", "promise"
C"start", "end", "nextTick", "promise", "setImmediate"
D"start", "nextTick", "end", "promise", "setImmediate"
Sign up free to play
Answer all 10 questions (7 more), see explanations for every answer, and track your score.