Challenge - 5 Problems
Spawn Streaming Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ component_behavior
intermediate2:00remaining
What is the output of this Node.js spawn code?
Consider this code snippet using
spawn from child_process. What will be printed to the console?Node.js
import { spawn } from 'child_process'; const ls = spawn('ls', ['-l']); ls.stdout.on('data', (data) => { console.log(`Output chunk: ${data.toString()}`); }); ls.stderr.on('data', (data) => { console.error(`Error chunk: ${data.toString()}`); }); ls.on('close', (code) => { console.log(`Process exited with code ${code}`); });
Attempts:
2 left
💡 Hint
Think about how spawn streams output and how events are emitted.
✗ Incorrect
The spawn function runs the 'ls -l' command and streams its output. The 'data' event on stdout emits chunks of the directory listing. When the process ends, the 'close' event fires with exit code 0 indicating success.
📝 Syntax
intermediate2:00remaining
Which option correctly spawns a process to stream output line-by-line?
You want to spawn a process and handle its output line-by-line as it streams. Which code snippet correctly sets this up?
Attempts:
2 left
💡 Hint
Remember that 'data' events emit buffers, not lines.
✗ Incorrect
The 'data' event emits chunks of data, which may contain multiple lines or partial lines. Splitting the chunk by newline characters lets you handle output line-by-line. There is no 'line' or 'chunk' event on stdout.
🔧 Debug
advanced2:00remaining
Why does this spawn code never print output?
This code spawns a process but never prints any output. What is the cause?
Node.js
import { spawn } from 'child_process'; const proc = spawn('node', ['-e', "console.log('hello')"]); proc.stdout.on('data', (data) => { console.log(`Output: ${data}`); });
Attempts:
2 left
💡 Hint
Check if the output is sent to stdout or stderr and if listeners are attached properly.
✗ Incorrect
The spawned node process runs a script that prints 'hello' to stdout. The listener on stdout 'data' event will receive this output and print it. The code works correctly and prints 'Output: hello'.
❓ state_output
advanced2:00remaining
What is the value of variable 'output' after this spawn code runs?
Given this code, what will be the final value of the 'output' variable after the process closes?
Node.js
import { spawn } from 'child_process'; let output = ''; const proc = spawn('echo', ['Hello World']); proc.stdout.on('data', (data) => { output += data.toString(); }); proc.on('close', () => { console.log('Process done'); });
Attempts:
2 left
💡 Hint
Remember what the echo command outputs including line breaks.
✗ Incorrect
The echo command outputs 'Hello World' followed by a newline character. The data event appends this string including the newline to 'output'.
🧠 Conceptual
expert2:00remaining
Which option best explains why spawn is preferred over exec for streaming large outputs?
Why do developers often choose
spawn instead of exec when they want to process large output streams from child processes?Attempts:
2 left
💡 Hint
Think about memory usage and how output is handled in both methods.
✗ Incorrect
spawn returns streams for stdout and stderr, allowing processing of output as it arrives. exec buffers the entire output in memory, which can cause issues with large outputs.