Home
← All questions

Cancelable promise in JavaScript

HardPremiumJavaScript

Asked at ThoughtSpot

JavaScript interview practice: Wrap a promise so it can be cancelled, rejecting with a custom cancellation error.

Problem

Implement makeCancelable(promise) that returns { promise, cancel }. Calling cancel() should make the returned promise reject with a cancellation marker, and the original resolution should be ignored after cancellation.

function makeCancelable(promise) {
  // return { promise, cancel }
}
Premium question

Unlock the in-browser editor, test cases, and model solution with Mentoxis Premium.

Unlock with Premium