Promises in JavaScript

Just a guy who loves to write code and watch anime.
Search for a command to run...

Just a guy who loves to write code and watch anime.
No comments yet. Be the first to comment.
Qualities that make outstanding builders.

Bipedal vs quadrupedal: how many legs This is about the number of legs the creature walks on. Bipedal. Two legs. Humans, ostriches, T-rex, kangaroos, most fantasy humanoids. Quadrupedal. Four legs. Do

Intro You hear "lerp" and "smoothstep" everywhere in game dev. They sound like math jargon. They're not. Both are small tools that do the same job: smoothly move from one value to another. The problem

What is Kinematics Kinematics is the math field. It's the study of how things move without worrying about forces (which would be dynamics). FK and IK are the two branches: forward kinematics and inver

Intro Textures are usually the biggest cost in a 3D scene. Memory, bandwidth, and load time all get eaten by them. Resizing them is the obvious lever. There's more. This post is about the less obvious

Promises in JavaScript are a powerful tool for managing asynchronous operations, allowing developers to write cleaner, more manageable code.
A Promise is an object representing the eventual completion or failure of an asynchronous operation. A Promise holds a value that might be available now, later, or never. It represents the idea of promising to do something. Promises have three states:
Pending: Initial state, neither fulfilled nor rejected.
Fulfilled: The operation completed successfully.
Rejected: The operation failed.
Consider the following example where a Promise resolves another Promise:
new Promise((resolveOuter) => {
resolveOuter(
new Promise((resolveInner) => {
setTimeout(resolveInner, 1000);
})
);
});
In this case, the outer Promise is pending until the inner Promise resolves. The completion of the inner Promise after 1 second fulfills the outer Promise.
Promises can be chained to perform sequential asynchronous operations. Methods like .then(), .catch(), and .finally() enable this chaining:
const myPromise = new Promise((resolve, reject) => {
setTimeout(() => {
resolve("foo");
}, 300);
});
myPromise
.then(handleFulfilledA)
.then(handleFulfilledB)
.then(handleFulfilledC)
.catch(handleRejected)
.finally(() => console.log('Operation completed.'));
.then() executes a callback on Promise fulfillment or rejection.
.catch() is a shorthand for handling rejections.
.finally() executes after the Promise settles, regardless of its outcome.
JavaScript provides several methods to deal with multiple Promises concurrently:
Promise.all() waits for all promises to be resolved or for any to be rejected. Helpful for combining the outcomes of several promises.
Promise.all([promise1, promise2]).then((results) => {
const [result1, result2] = results;
console.log(result1, result2);
});
Promise.allSettled() waits for all promises to settle, regardless of the outcome. Each promise's result is provided, indicating success or failure.
Promise.allSettled([promise1, promise2]).then((results) => results.forEach((result) => console.log(result.status)));
Promise.race() waits for the first promise to settle, either fulfilled or rejected. This is useful for timeout patterns.
Promise.race([promise1, promise2]).then((result) => console.log(result));
Promise.any() waits for the first promise to fulfill, ignoring all rejections unless all promises are rejected.
Promise.any([promise1, promise2]).then((result) => console.log(result));
Async/await syntax offers a cleaner, more readable way to work with Promises, making asynchronous code appear synchronous:
async function fetchData() {
try {
const data = await fetch('https://api.example.com/data');
console.log(await data.json());
} catch (error) {
console.error('Failed to fetch data:', error);
}
}
An async function implicitly returns a Promise.
The await keyword pauses the function execution until the Promise settles.
Use try/catch blocks to handle potential rejections.
To run Promises concurrently within an async function, utilize Promise.all():
async function fetchMultipleData() {
try {
const [dataA, dataB] = await Promise.all([fetchDataA(), fetchDataB()]);
console.log(dataA, dataB);
} catch (error) {
console.error('Failed to fetch data:', error);
}
}
This approach ensures that dataA and dataB are fetched in parallel, optimizing performance.