On this page
ES Modules & Error Handling
Prev: Event Loop & Async Programming | Up: tutorials | Next: Iterators, Generators & Iterator Helpers
Rel: Interactive & Scripting, Promise & Async
Ref: MDN Web Docs — JavaScript Modules
Tags: #public #web #javascript #tutorials #modules #errors
Contents
Static & Dynamic ES Modules
ES Modules run in strict mode, have their own module scope, execute once per specifier, and support top-level await:
// math.js
export const PI = 3.14159;
export function area(r) { return PI * r * r; }
// app.js
import { area } from "./math.js";
// Lazy dynamic import (returns a Promise)
const mod = await import("./math.js");
Custom Errors & Error.cause
Wrap low-level errors with { cause } so root-cause diagnostics are preserved:
class ApiError extends Error {
constructor(message, { status, cause } = {}) {
super(message, { cause });
this.name = "ApiError";
this.status = status;
}
}
try {
JSON.parse("{bad");
} catch (err) {
throw new ApiError("Failed to decode payload", { status: 400, cause: err });
}
Linked from
- tuts/javascript/tutorials
- tuts/javascript/tutorials/event_loop_and_async_programming
- tuts/javascript/tutorials/iterators_and_generators
- tuts/javascript/tutorials/modules_and_error_handling
- tuts/programming/software_design/error_handling_philosophies
- tuts/typescript/builtins
- tuts/typescript/builtins/namespace
- tuts/typescript/tsconfig_and_tooling
- tuts/typescript/tsconfig_and_tooling/declaration_files_and_interop
- tuts/web/tutorials/devtools_and_debugging
Related notes
- tuts/javascript/tutorials Linked here; links to this page; shared tags: #javascript, #tutorials, #web
- tuts/javascript/tutorials/event_loop_and_async_programming Linked here; links to this page; shared tags: #javascript, #tutorials, #web
- tuts/javascript/tutorials/iterators_and_generators Linked here; links to this page; shared tags: #javascript, #tutorials, #web
- tuts/javascript/builtins/promise_and_async Linked here; shared tags: #javascript, #web
- tuts/web/tutorials/devtools_and_debugging links to this page; shared tags: #tutorials, #web
- tuts/html/elements/interactive_and_scripting Linked here; shared tags: #web
- tuts/programming/software_design/error_handling_philosophies links to this page; shared tags: #errors
- tuts/typescript/builtins links to this page; shared tags: #web
- tuts/typescript/builtins/namespace links to this page; shared tags: #web
- tuts/typescript/tsconfig_and_tooling links to this page; shared tags: #web