DevPrep
  • Interview Prep
  • Projects
  • Resources
  • Pricing
  • About Us
Submit Question
DevPrep
  • Pricing
  • About Us
Submit Question
← From Beginner to Expert in Fundamentals of Frontend
  • Scope & Closures
  • Asynchronous JavaScript
  • DOM Manipulation
  • ES6+ to 2026
  • Execution Context & Call Stack
  • Hoisting: var, let, const & Functions
  • Data Types & Type Coercion
  • Prototypes & the this Keyword
  • The Event Loop Deep Dive
  • Higher-Order Functions, Currying & Functional Patterns
  • Callbacks, Promises & Async/Await
  • Event Delegation & DOM Optimization
  • Destructuring, Spread/Rest & Modern Syntax
  • ES Modules & the Fetch API
  • Memory Management & Garbage Collection
  • Debouncing, Throttling & Performance Patterns
  • Design Patterns in JavaScript
  • Proxies, Reflect & Symbols
  • JavaScript Optimizations & Best Practices
  • JavaScript Interview Questions (30 MCQs)
← From Beginner to Expert in Fundamentals of Frontend

ES Modules & the Fetch API

18 min read
ES ModulesCommonJSDynamic ImportFetch APIAbortControllerStreamingTree Shaking

ES Modules vs CommonJS

FeatureES ModulesCommonJS
Syntaximport/exportrequire/module.exports
LoadingStatic (at parse time)Dynamic (at runtime)
Tree Shaking✅ Yes❌ No
Top-level await✅ Yes❌ No
Browser Support✅ Native❌ Needs bundler
BindingLive bindingsValue copies
// Named exports
export const PI = 3.14159;
export function add(a, b) { return a + b; }

// Default export
export default class Calculator { ... }

// Import
import Calculator, { PI, add } from "./math.js";

// Dynamic import (code splitting)
const module = await import("./heavy-module.js");

// Re-export
export { default as Button } from "./Button.js";
export * from "./utils.js";

The Fetch API

// Basic GET
const response = await fetch("https://api.example.com/users");
if (!response.ok) throw new Error(`HTTP ${response.status}`);
const data = await response.json();

// POST with JSON body
const result = await fetch("/api/users", {
  method: "POST",
  headers: { "Content-Type": "application/json" },
  body: JSON.stringify({ name: "Alice", email: "alice@example.com" }),
});

// AbortController (cancel requests)
const controller = new AbortController();
setTimeout(() => controller.abort(), 5000); // 5s timeout

try {
  const response = await fetch("/api/slow", {
    signal: controller.signal,
  });
} catch (err) {
  if (err.name === "AbortError") {
    console.log("Request was cancelled");
  }
}

// Streaming response
const response = await fetch("/api/large-file");
const reader = response.body.getReader();
while (true) {
  const { done, value } = await reader.read();
  if (done) break;
  processChunk(value);
}

Fetch Gotchas

  • fetch does NOT reject on HTTP errors (404, 500). Only network failures cause rejection.
  • response.json() can only be called once (the body is consumed).
  • Cookies require credentials: "include" for cross-origin requests.

Practice

  • JavaScript
  • DSA
  • Machine Coding
  • System Design

Resources

  • Learning Tracks
  • Articles
  • Roadmaps
  • Compare Concepts
  • Glossary
  • Developer Tools
  • All Questions

Company

  • About
  • Pricing

Legal

  • Privacy Policy
  • Terms of Service
DevPrep

© 2026 DevPrep. All rights reserved.