InkdownInkdown
Start writing

Study

59 filesยท8 subfolders

Shared Workspace

Study
core

Event loop

Shared from "Study" on Inkdown

Think of the event loop like a restaurant kitchen with one chef ๐Ÿณ๐Ÿ‘จโ€๐Ÿณ

The Simple Story

๐Ÿง‘โ€๐Ÿณ The Chef (Call Stack)
  • Only one chef exists (JavaScript is single-threaded)
  • The chef can only cook one dish at a time
  • Dishes stack up on the counter (call stack)
๐Ÿ“ž The Waiter (Web APIs)
  • Takes orders that take time (timers, fetch requests, file reads)
  • Gives them to the kitchen staff in the back (browser/Node.js)
  • Doesn't make the chef wait โ€“ the chef keeps cooking other dishes
๐Ÿ“‹ The Priority List (Microtask Queue)
  • VIP orders (Promises) โ€“ must be done immediately after current dish
  • Chef clears ALL VIP orders before touching regular orders
๐Ÿ“ The Regular List (Task Queue)
  • Normal orders (setTimeout, clicks, I/O)
programming-language-concepts.md
zero-language-explanation.md
DB
01-introduction.md
02-relational-databases.md
03-database-design.md
04-indexing.md
05-transactions-acid.md
06-nosql-databases.md
07-query-optimization.md
08-replication-ha.md
09-sharding-partitioning.md
10-caching-strategies.md
11-cap-theorem.md
12-connection-pooling.md
13-backup-recovery.md
14-monitoring.md
15-database-selection.md
README.md
JS
Event loop
Merlin Backend
01-Orchestration.md
02-DeepResearch.md
03-Search.md
04-Scraping.md
05-Streaming.md
06-MultiProviderLLM.md
07-MemoryAndContext.md
08-ErrorHandling.md
09-RateLimiting.md
10-TaskQueue.md
11-SecurityAndAuth.md
Orchestration-2nd-draft
OpenAI Agents Python
00_OVERVIEW.md
01_AGENT_SYSTEM.md
02_RUNNER_SYSTEM.md
03_TOOL_SYSTEM.md
04_ITEMS_SYSTEM.md
05_GUARDRAILS.md
06_HANDOFFS.md
07_MEMORY_SESSIONS.md
08_MODEL_PROVIDERS.md
09_SANDBOX_SYSTEM.md
10_TRACING.md
11_RUN_STATE.md
12_CONTEXT.md
13_LIFECYCLE_HOOKS.md
14_CONFIGURATION.md
15_ERROR_HANDLING.md
16_STREAMING.md
17_EXTENSIONS.md
18_MCP_INTEGRATION.md
19_BEST_PRACTICES.md
20_ARCHITECTURE_PATTERNS.md
opencode-study
context-handling
core
Python
Alembic
Basics
sqlalchemy - fastapi
SQLAlchemy overview
tweets
system_design_for_agentic_apps.md
  • Chef picks one from this list only when VIP list is empty
  • ๐Ÿ”„ The Event Loop (The Manager)
    • Constantly checks: "Is the chef free?"
    • If yes: Gives next task (VIPs first, then regular)
    • If no: Waits
    • Repeats forever while the restaurant is open

    Real Code Example

    JavaScript
    console.log('๐Ÿ• Order pizza'); // Chef cooks immediately
    
    setTimeout(() => { // Waiter takes this to back kitchen
      console.log('๐ŸŸ Fries ready');
    }, 0);
    
    Promise.resolve().then(() => { // VIP order!
      console.log('๐Ÿฅค Drink served');
    });
    
    console.log('๐Ÿ” Order burger'); // Chef cooks immediately

    Mermaid Diagram

    Mermaid
    flowchart TD
        A[Start script] --> B[Run synchronous code]
        B --> C[Async APIs complete]
        C --> D[Promise callbacks -> Microtask Queue]
        C --> E[setTimeout/click/I O callbacks -> Task Queue]
        D --> F{Call Stack empty?}
        E --> F
        F -->|No| G[Wait]
        G --> F
        F -->|Yes| H[Event Loop checks queues]
        H --> I[Run all Microtasks first]
        I --> J[Run one Task]
        J --> F