liveBook · Manning

Promise

As explained in JavaScript in Depth

A JavaScript object representing the eventual completion or failure of an asynchronous operation and its resulting value.

A promise represents an asynchronous result, but its observable behavior depends on more than the promise object itself. ECMAScript defines promises, while the JavaScript engine and host runtime influence scheduling and event ordering. section 1.3.3 section 8.0

A promise resolution schedules reactions through the microtask queue. Those reactions can run after a callback returns but before later task processing, creating timing bugs when promise-based work is mixed with host events. Runtime-specific rules also affect when rejected promises are classified as unhandled. section 8.5.4 section 10.4.5

Stream completion before async processing

An asynchronous data callback may still be waiting in the microtask queue when a stream emits its end event, so results.length can be zero or incomplete. Track pending promises and await Promise.all, or consume the stream with for await...of before collecting results. section 8.5.4

Promise allocation at scale

Web Streams create promises for reading, writing, and backpressure. In CPU-bound pipelines, repeated allocations, retained results, and microtask processing can dominate performance; in I/O-bound pipelines, the cost may be less visible. section 10.4.5

for await (const chunk of readable) {
  pending.push(transform(chunk));
}
// We don’t care what order the transforms complete
 
const results = await Promise.all(pending);
console.log(`Processed ${results.length} chunks`);

Use the async-iterable example to compare explicit stream events with promise collection and Promise.all. section 8.5.4

How JavaScript in Depth covers it

How deep it goes
Explains internal state, reactions, microtasks, retention, and runtime timing, but defers complete implementation details.
Examples
Uses premature stream completion, pending Promise.all tracking, async iteration, and runtime-dependent unhandled rejection timing.
What it assumes
Assumes familiarity with asynchronous functions, callbacks, streams, and event scheduling while distinguishing language, engine, and host behavior.
What you can do after
Diagnose timing bugs by tracking pending work explicitly and choosing async iteration or Promise.all for stream processing.
How it connects
Connects promises with microtask queues, event loops, streams, async iteration, Promise.all, backpressure, and unhandled rejection detection.

Also explained in

Compare how 11 books explain promise →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of JavaScript in Depth. The section links point to the parts of the book it draws on.