Understanding JavaScript Promises: Concepts, API, and Practical Examples

This article explains JavaScript Promises, covering their purpose in avoiding callback hell, the core API (constructor, then, catch, static methods), practical code examples, chaining behavior, comparison with callbacks and async/await, and even a custom implementation, providing a comprehensive guide for frontend developers.

Sohu Tech Products
Sohu Tech Products
Sohu Tech Products
Understanding JavaScript Promises: Concepts, API, and Practical Examples

Promises are a fundamental abstraction for handling asynchronous operations in JavaScript, eliminating the “callback hell” problem that arises with nested AJAX calls.

The article first reviews callback functions, then introduces why Promises are needed, followed by a detailed description of the Promise API: the constructor, instance methods then and catch, and static methods Promise.resolve, Promise.reject, Promise.all, and Promise.race.

Several code snippets illustrate creating a Promise, chaining calls, handling errors, and wrapping AJAX requests:

var promise = new Promise((resolve, reject) => {
    if (true) { resolve(100); }
    if (false) { reject('error'); }
});
promise.then(value => console.log(value))
       .catch(error => console.error(error));

Further examples show how then returns a new Promise, enabling sequential data flow, and how Promise.all and Promise.race coordinate multiple asynchronous tasks.

Promise.all([timerPromisefy(1), timerPromisefy(32)])
       .then(values => console.log(values)); // [1,32]
Promise.race([timerPromisefy(1), timerPromisefy(32)])
       .then(value => console.log(value)); // 1

The article also compares callbacks, Promises, and async/await, highlighting the advantages of each approach, and provides a minimal custom implementation of a Promise class to deepen understanding of its internal mechanics.

class MyPromise {
    constructor(handle) { /* ... */ }
    then(onFulfilled, onRejected) { /* ... */ }
    catch(onRejected) { return this.then(undefined, onRejected); }
    static resolve(value) { /* ... */ }
    static reject(value) { /* ... */ }
    static all(list) { /* ... */ }
    static race(list) { /* ... */ }
}
Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

JavaScriptAsynchronouscallbackPromise
Sohu Tech Products
Written by

Sohu Tech Products

A knowledge-sharing platform for Sohu's technology products. As a leading Chinese internet brand with media, video, search, and gaming services and over 700 million users, Sohu continuously drives tech innovation and practice. We’ll share practical insights and tech news here.

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.