Stop using Promise.all() in JavaScript
Published in
4 min readAug 14, 2023
What are Promises in JavaScript
If you’ve stumbled upon this article, you’re likely familiar with promises. However, for those new to JavaScript, let’s break it down. Essentially, a Promise
object signifies the eventual completion or failure of an asynchronous operation. Its value, intriguingly, may not be immediately available when the promise is created.
const promise = new Promise((resolve, reject) => {
// Some asynchronous operation
if (/* operation is successful */) {
resolve(result);
} else {
reject(error);
}
});
They have 3 states:
- Pending: This is the initial state, which is neither fulfilled nor rejected
- Fulfilled: The state when the promise completes successfully and resulting in value
- Rejected: The state when an error occurs and the operation in the promise is not successful
Once a promise is settled, you can employ .then()
to handle the result and .catch()
to manage any errors that arise during its execution.
promise
.then(result => {
console.log(‘Success:’, result);
})
.catch(error => {
console.error(‘Error:’, error);
});