惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

博客园 - Franky
云风的 BLOG
云风的 BLOG
人人都是产品经理
人人都是产品经理
博客园 - 叶小钗
Engineering at Meta
Engineering at Meta
Vercel News
Vercel News
Y
Y Combinator Blog
B
Blog
Microsoft Azure Blog
Microsoft Azure Blog
C
Check Point Blog
M
MIT News - Artificial intelligence
Jina AI
Jina AI
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
Apple Machine Learning Research
Apple Machine Learning Research
Hugging Face - Blog
Hugging Face - Blog
阮一峰的网络日志
阮一峰的网络日志
罗磊的独立博客
Stack Overflow Blog
Stack Overflow Blog
F
Fortinet All Blogs
博客园 - 司徒正美
I
InfoQ
Google DeepMind News
Google DeepMind News
GbyAI
GbyAI
U
Unit 42

DEV Community

Authentication Security Deep Dive: From Brute Force to Salted Hashing (With Java Examples) Why AI Systems Don’t Fail — They Drift Spilling beans for how i learn for exam😁"Reinforcement Learning Cheat Sheet" I Replaced Chrome with Safari for AI Browser Automation. Here's What Broke (and What Finally Worked) How Python Borrows Other People's Work The $40 Architecture: Processing 1 Billion API Requests with 99.99% Uptime Vibe Coding: A Workflow Guide (From Zero to SaaS) Most webhook security guides protect the wrong side. The scary part is delivery. Headless CMS for TanStack Start: Build a Blog with Cosmic EU Age Verification App "Hacked in 2 Minutes" — What Actually Happened Comfy Cloud’s delete function does not actually remove files Running AI Models on GPU Cloud Servers: A Beginner Guide Event-driven media intelligence with AWS Step Functions and Bedrock I scored 500 AI prompts across 8 quality dimensions — here's what broke How to Call Google Gemini API from Next.js (Free Tier, No Backend Needed) The Portal Protocol: Reclaiming Human Connection in the Age of AI How to Fix Your Team's Scattered Knowledge Problem With a Self-Hosted Forum Intro to tc Cloud Functors: A Graph-First Mental Model for the Modern Cloud Designing Multi-Tenant Backends With Both Ownership and Team Access I Built a Neumorphic CSS Library with 77+ Components — Here's What I Learned PostgreSQL Performance Optimization: Why Connection Pooling Is Critical at Scale Cómo construí un SaaS multi-rubro para gestionar expensas en Argentina con FastAPI + Vue 3 🚀 I Built an Ethical Hacking Scanner Tool – Open Source Project I Replaced /usage and /context in Claude Code With a Single Statusline A Pythonic Way to Handle Emails (IMAP/SMTP) with Auto-Discovery and AI-Ready Design I Collected 8.9 Million Polymarket Price Points — Here's What I Found About How Markets Really Move EcoTrack AI — Carbon Footprint Tracker & Dashboard Everyone's Using AI. No One Agrees How. 5 self-hosted ebook managers worth trying in 2026 Building Your First AI Agent with LangChain: From Chatbot to Autonomous Assistant
Stop Treating setTimeout(fn, 0) Like Magic
Paschal Ogbu · 2026-05-22 · via DEV Community
Cover image for Stop Treating setTimeout(fn, 0) Like Magic

Paschal Ogbu

TL;DR: setTimeout(fn, 0) doesn't run code instantly — it defers execution to the Macrotask queue, after all Microtasks and pending renders. Here's why relying on it is an anti-pattern and what to use instead.

We've all seen it in legacy frontend codebases or quick hotfixes. A piece of UI isn't rendering correctly, or a DOM element isn't ready yet, so a developer drops this in:
jssetTimeout(() => { doSomething(); }, 0);
It feels like magic because suddenly, the race condition disappears and the bug is solved. But do you actually know why it worked, or what it just did to your browser's execution priorities?
To master frontend engineering at scale, you have to look under the hood at the JavaScript Event Loop.
The Mechanics: Microtasks vs. Macrotasks
The browser processes asynchronous JavaScript using two distinct queues:
Microtask Queue: Handles Promises (.then), async/await, and MutationObserver.
Macrotask Queue (Callback Queue): Handles setTimeout, setInterval, and user interactions.
The Event Loop has a strict rule: It will completely empty the Microtask Queue before it picks up even ONE task from the Macrotask Queue.
Don't believe me? ⬆️ Run that code in your console and see for yourself.
When you run a setTimeout with 0 milliseconds, you aren't running code instantly. You are intentionally telling the browser: "Take this function, push it to the back of the task queue, allowing pending microtasks and potentially a render cycle to complete first."
Why relying on this is an architectural anti-pattern:
Using setTimeout(0) to bypass race conditions is like putting tape over a check engine light. It masks architectural flaws — usually meaning your component state is poorly synchronized, or you are trying to manipulate the DOM before a framework's render lifecycle is fully complete.
Use these instead:

requestAnimationFrame — for aligning code execution with browser paint cycles
Framework lifecycle hooks — for DOM-ready execution
Proper state management — for synchronization issues

Modern frontend engines give us cleaner tools. If you need to align code execution with browser layout paints, look toward native utilities like requestAnimationFrame or framework-specific dependency tracking rather than relying on timers.
🛠️ I've documented these fundamental browser performance sequences and added a live tracking script to an open-source frontend architecture repository:
🔗 https://github.com/Passyswatz/frontend-mastery-notes
Feel free to clone it, test your own async code, and bookmark it for your team.