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

πŸ›‘ Cancelable Promise Implementation

A wrapper that prevents promise handlers from executing after cancellation. Useful for avoiding state updates in unmounted components.

A cancelable promise wrapper allows you to prevent promise handlers from executing after cancellation. This is useful for avoiding state updates in unmounted React components or canceling outdated API responses. -- βœ… Implementation -- πŸ§ͺ Example: -- βœ… Behavior: If is called before resolution, the promise rejects with . If not canceled, resolves normally. Doesn’t abort the underlying task β€” only prevents / from running. -- 🧠 Notes: This doesn't stop network/fetch/etc. β€” only suppresses result handlers . Use for cancelable fetch requests or actual task termination. -- <!-quiz-start --Q1: What does calling cancel() on a cancelable promise actually do? [ ] It immediately aborts the underlying async operation [ ] It throws an error that stops the promise execution [x] It prevents the .then() and .catch() handlers from executing normally [ ] It removes the promise from memory Q2: What happens when a cancelable promise is canceled before resolution? [ ] The promise resolves with null [ ] The original promise is destroyed [x] The promise rejects with [ ] The promise remains pending forever Q3: For truly canceling a fetch request (not just ignoring results), what should you use? [ ] Promise.cancel() [ ] clearTimeout() [x] AbortController [ ] Promise.reject() <!-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
4 of 8
LibraryJavaScriptPromises43 of 61

πŸ›‘ Cancelable Promise Implementation

jspromisesmedium

A cancelable promise wrapper allows you to prevent promise handlers from executing after cancellation. This is useful for avoiding state updates in unmounted React components or canceling outdated API responses.


βœ… Implementation

function makeCancelable(promise) {
  let hasCanceled = false;

  const wrapped = new Promise((resolve, reject) => {
    promise
      .then((val) => (hasCanceled ? reject({ canceled: true }) : resolve(val)))
      .catch((err) => (hasCanceled ? reject({ canceled: true }) : reject(err)));
  });

  return {
    promise: wrapped,
    cancel() {
      hasCanceled = true;
    }
  };
}

πŸ§ͺ Example:

const task = new Promise((res) => setTimeout(() => res("Done"), 1000));
const cancelable = makeCancelable(task);

cancelable.promise
  .then(console.log)
  .catch((err) => {
    if (err.canceled) console.log("Canceled");
    else console.error(err);
 });

setTimeout(() => cancelable.cancel(), 500); // cancel before it resolves

βœ… Behavior:

  • If .cancel() is called before resolution, the promise rejects with { canceled: true }.
  • If not canceled, resolves normally.
  • Doesn’t abort the underlying task β€” only prevents .then()/.catch() from running.

🧠 Notes:

  • This doesn't stop network/fetch/etc. β€” only suppresses result handlers .
  • Use AbortController for cancelable fetch requests or actual task termination.

Quick Quiz

Test your understanding with 3 quick questions

Q1What does calling cancel() on a cancelable promise actually do?
Q2What happens when a cancelable promise is canceled before resolution?
Q3For truly canceling a fetch request (not just ignoring results), what should you use?

Continue Reading

CrackFrontend

Your guide to mastering frontend interviews

Resources

LibraryPracticeDonate

Company

About UsContact

Legal

Privacy PolicyTerms of Service

Built with ❀️ by Tushar Khanna