Key Takeaways
-
Async/await helps with I/O, not CPU computation
- I/O = waiting for external operations (files, network, database)
- CPU = actively computing on the main thread
-
The event loop has 6 phases + microtasks
- Microtasks (nextTick, Promises) run between every phase
- Understanding this helps debug timing issues
-
For CPU-intensive tasks:
- Use Worker Threads for true parallelism
- Break up work with
setImmediatefor simpler cases - Never block the main thread
-
setImmediate always runs in the next loop iteration
- Use it to give the event loop breathing room
- Multiple calls in same loop run together in next iteration
-
Modern async patterns:
- Use
async/awaitfor readability - Use
Promise.all()for parallel operations - Always handle errors with try/catch
- Use