šŸš€ LEVEL UP TO SENIOR:Unlock 500+ Advanced Practical Challenges & Exercises.
šŸŽ“ COURSERA PARTNER:Earn professional Google, Meta, and IBM certificates to supercharge your resume.
HTML MASTER CLASS /// LEARN TAGS /// BUILD STRUCTURE /// SEMANTIC WEB /// HTML MASTER CLASS /// LEARN TAGS ///

Untitled Lesson

⚔ Total XP: 0|šŸ’» backend XP: 0

Skill Matrix

UNLOCK NODES BY LEARNING NEW TAGS.

Select an unlocked node to view details root

Pascual Vila

Pascual Vila

Frontend Instructor // Code Syllabus

Common Pitfalls & Errors

The Error //

Emitting an 'error' event with no listener attached, crashing the entire process

// Wrong: crashes the entire Node process myEmitter.emit('error', new Error('DB connection lost')); // Correct: always have a listener first myEmitter.on('error', (err) => console.error('Handled safely:', err)); myEmitter.emit('error', new Error('DB connection lost'));

The Solution //

Node treats 'error' as a special event name — if an EventEmitter emits 'error' and there is no .on('error', ...) listener registered anywhere on that emitter, Node throws the error as an uncaught exception and terminates the whole process, not just that one operation. Always attach an error listener to any EventEmitter that might emit one, even if it just logs.

The Error //

Assuming .emit() runs listeners asynchronously, in the background

// Wrong: assumes emit() doesn't block myEmitter.on('heavy', () => { for (let i = 0; i < 1e9; i++) {} }); myEmitter.emit('heavy'); console.log('This waits for the loop above to finish!');

The Solution //

Unlike a Promise or setTimeout, EventEmitter listeners execute synchronously, in registration order, on the same call stack as .emit() itself — code after .emit() does not run until every listener has finished. A listener doing heavy synchronous work will block the event loop just like any other blocking code, and a listener that itself needs to do async work should use async/await internally rather than assuming the emitter parallelizes it.

Continue Learning