AceDevHub
Async JavaScriptFree

Promises Fundamentals

Represent future values with Promises — pending, fulfilled, rejected — and chain async steps with .then and .catch.

IntermediateFreePromises

A Promise is an object representing the eventual result of async work — success or failure. It standardizes what callbacks did ad hoc: one success path, one error path, and composable chaining. fetch returns a Promise; database drivers and AWS SDKs expose Promise APIs. Once you read .then chains fluently, async/await is a thin syntax layer on top.

Three states — pending, fulfilled, rejected

A Promise starts pending. It settles once to fulfilled (with a value) or rejected (with a reason, usually an Error). Settled promises never change state. .then registers handlers for fulfillment; .catch handles rejection. Forgetting .catch on a chain creates unhandled rejection warnings in Node and failed silent bugs in production.

promise-states.js
Loading editor…
Outputconsole
Promise { <pending> }
Rejected: Network down
Fulfilled: done

Creating promises and wrapping callbacks

new Promise((resolve, reject) => { ... }) wraps callback-style APIs. Call resolve(value) once on success, reject(error) once on failure — calling both is ignored after the first settlement. util.promisify in Node automates this for error-first callbacks. Modern APIs skip callbacks entirely and return promises natively.

wrap-callback.js
Loading editor…
Outputconsole
waited 100ms
3

Chaining .then — sequential async flow

Each .then returns a new Promise. Return a value from .then to pass it to the next .then. Return a Promise from .then to flatten async sequencing — the chain waits for that inner promise. Throwing inside .then rejects the chain and skips to .catch. This flat chain replaces nested callbacks for step-by-step workflows.

promise-chain.js
Loading editor…
Outputconsole
Logged in
Hello Sangam
Cleanup runs win or lose

fetch and HTTP — promises in the wild

fetch(url) resolves when headers arrive — even for HTTP 404. It rejects only on network failure. Check response.ok before parsing JSON. This catches junior developers who assume catch handles 404. Always handle both network errors (catch) and HTTP errors (manual status check).

fetch-pattern.js
Loading editor…

Promises are the interchange format between callback APIs, fetch, database drivers, and async/await syntax. Treat every async function as returning a promise you must handle — fire-and-forget async calls are the primary source of unhandled rejection incidents in Node services.

  1. 1Promises settle once — fulfilled or rejected
  2. 2Always attach .catch or try/catch with await
  3. 3Return values/promises from .then for chaining
  4. 4fetch resolves on HTTP errors — check response.ok
  5. 5Next lesson: async/await syntax