Building Type-Safe APIs with Nuxt 3 and Prisma
Learn how to build end-to-end type-safe APIs using Nuxt 3 server routes and Prisma ORM for a seamless developer experience.
A deep dive into the JavaScript event loop, microtasks, macrotasks, and how they affect your application's performance.
Mbeah Essilfie
July 15, 2026 at 06:02 AM
JavaScript is single-threaded, but that doesn't mean it can only do one thing. The event loop is the mechanism that makes asynchronous operations possible without threads.
console.log('1: Synchronous')
setTimeout(() => console.log('2: Macrotask'), 0)
Promise.resolve().then(() => console.log('3: Microtask'))
queueMicrotask(() => console.log('4: Microtask via queueMicrotask'))
console.log('5: Synchronous')
Output:
1: Synchronous
5: Synchronous
3: Microtask
4: Microtask via queueMicrotask
2: Macrotask
After each macrotask completes, the engine drains all microtasks before moving to the next macrotask or rendering.
// BAD: Blocks the main thread
function processItems(items: Item[]) {
items.forEach(item => heavyComputation(item))
}
// GOOD: Yields to the event loop between chunks
async function processItemsChunked(items: Item[], chunkSize = 50) {
for (let i = 0; i < items.length; i += chunkSize) {
const chunk = items.slice(i, i + chunkSize)
chunk.forEach(item => heavyComputation(item))
// Yield to allow UI updates and other tasks
await new Promise(resolve => setTimeout(resolve, 0))
}
}
In Node.js, the event loop has additional phases:
// setImmediate vs setTimeout in Node.js
setImmediate(() => console.log('immediate'))
setTimeout(() => console.log('timeout'), 0)
// Order is non-deterministic at top level!
// But inside an I/O callback, setImmediate always fires first:
const fs = require('fs')
fs.readFile(__filename, () => {
setImmediate(() => console.log('immediate')) // Always first
setTimeout(() => console.log('timeout'), 0)
})
Fullstack Software Developer
Learn how to build end-to-end type-safe APIs using Nuxt 3 server routes and Prisma ORM for a seamless developer experience.
Go beyond basic utility classes and learn how to build cohesive, maintainable design systems with Tailwind CSS.
Understand the power of Vue 3's Composition API through practical, real-world composable patterns that you can use today.
