CrackFrontendCF
Resources
Practice
CrackFrontend

Your guide to mastering frontend interviews

Resources

LibraryPracticeDonate

Company

About UsContact

Legal

Privacy PolicyTerms of Service

Built with ❤️ by Tushar Khanna

CrackFrontendCF
Resources
Practice

✅ Promise.allSettled() Implementation

Promise.allSettled waits for all promises to settle and returns their outcomes. Never rejects, always returns success and error results.

returns a promise that resolves after all input promises have settled (either fulfilled or rejected). Unlike , it never rejects—it always returns an array of result objects describing each promise's outcome. -- 🧠 Understanding Promise.allSettled -- Atom 1: Purpose returns a promise that: ✅ Resolves when all input promises settle (either resolve or reject) ❌ Never rejects — result always contains an array of outcomes -- Atom 2: Result Format Each result is an object with shape: If resolved: If rejected: -- Atom 3: Normalize Inputs Use to handle non-promise inputs. -- Atom 4: Track Completion Maintain Track how many have settled Resolve once all have settled -- Atom 5: Edge Case Empty input → resolve to immediately -- ✅ Implementation: -- 🧪 Example: -- <!-quiz-start --Q1: What is the key difference between Promise.all and Promise.allSettled? [ ] Promise.allSettled runs promises sequentially [ ] Promise.allSettled is faster than Promise.all [x] Promise.allSettled never rejects and returns all outcomes regardless of failures [ ] Promise.allSettled only works with async/await syntax Q2: What is the result format for a rejected promise in Promise.allSettled? [ ] [ ] [x] [ ] Q3: What does Promise.allSettled return when passed an empty array? [ ] It throws an error [ ] It returns a promise that never settles [x] It resolves immediately with an empty array [] [ ] It rejects with an AggregateError <!-quiz-end --
JavaScriptPromises
🎯 Promise.all() Implementation
hard
✅ Promise.allSettled() Implementation
medium
🎯 Promise.any() Implementation
medium
🛑 Cancelable Promise Implementation
medium
🔧 Custom Promise Class Implementation
hard
🏁 Promise.race() Implementation
medium
🔄 Promise Retry Implementation
easy
📋 Sequential Promise Execution
medium
2 of 8
LibraryJavaScriptPromises41 of 61

✅ Promise.allSettled() Implementation

jspromisesmedium

Promise.allSettled() returns a promise that resolves after all input promises have settled (either fulfilled or rejected). Unlike Promise.all(), it never rejects—it always returns an array of result objects describing each promise's outcome.


🧠 Understanding Promise.allSettled


Atom 1: Purpose

Promise.allSettled returns a promise that:

  • ✅ Resolves when all input promises settle (either resolve or reject)
  • ❌ Never rejects — result always contains an array of outcomes

Atom 2: Result Format

Each result is an object with shape:

  • If resolved: { status: "fulfilled", value: result }
  • If rejected: { status: "rejected", reason: error }

Atom 3: Normalize Inputs

Use Promise.resolve(...) to handle non-promise inputs.


Atom 4: Track Completion

  • Maintain results[]
  • Track how many have settled
  • Resolve once all have settled

Atom 5: Edge Case

  • Empty input → resolve to [] immediately

✅ Implementation: promiseAllSettled

function promiseAllSettled(iterable) {
  return new Promise((resolve) => {
    const promises = Array.from(iterable);
    const results = [];
    let settledCount = 0;

    if (promises.length === 0) return resolve([]);

    promises.forEach((p, i) => {
      Promise.resolve(p)
        .then(value => {
          results[i] = { status: "fulfilled", value };
        })
        .catch(reason => {
          results[i] = { status: "rejected", reason };
        })
        .finally(() => {
          settledCount++;
          if (settledCount === promises.length) {
            resolve(results);
          }
        });
    });
  });
}

🧪 Example:

promiseAllSettled([
  Promise.resolve(1),
  Promise.reject("fail"),
  3
]).then(console.log);

/*
[
  { status: "fulfilled", value: 1 },
  { status: "rejected", reason: "fail" },
  { status: "fulfilled", value: 3 }
]
*/

Quick Quiz

Test your understanding with 3 quick questions

Q1What is the key difference between Promise.all and Promise.allSettled?
Q2What is the result format for a rejected promise in Promise.allSettled?
Q3What does Promise.allSettled return when passed an empty array?

Continue Reading

CrackFrontend

Your guide to mastering frontend interviews

Resources

LibraryPracticeDonate

Company

About UsContact

Legal

Privacy PolicyTerms of Service

Built with ❤️ by Tushar Khanna