// See https://dom.spec.whatwg.org/#abortcontroller-api-integration function getData({signal}) { // 1. Let p be a new promise. // 4. Return p. return new Promise((resolve, reject) => { // 2. If options' signal member is present, then: if (signal && signal.aborted) { // 2. 1. If options' signal's aborted flag is set, then reject p with an "AbortError" DOMException and return p. throw new DOMException("Aborted", "AbortError"); } // 3. Run these steps in parallel: // 3. 1. Let amazingResult be the result of doing some amazing things. const id = setTimeout(() => { // 3. 2. Resolve p with amazingResult. resolve("data!") }, 1000); // 2. 2. Add the following abort steps to options’ signal: signal && signal.addEventListener("abort", () => { // 2. 2. 1. Stop doing amazing things. clearTimeout(id); // 2. 2. 2. Reject p with an "AbortError" DOMException. reject(new DOMException("Aborted", "AbortError")); }); }); } const controller = new AbortController(); const promise = getData({signal: controller.signal}); controller.abort();