Zrušenie asynchrónne postup je stále ešte nie je triviálna úloha, najmä ak potrebujete hlboké zrušenie a flow control. Nie je natívne riešenie v súčasnosti. Všetko, čo môžete urobiť natívne:
- prejsť AbortController stupňa, aby každý vnorené async funkciu, ktorú chcete, aby cancellable
- vyžiadať všetky vnútorné micro-úlohy (žiadosti, merače času, atď.) na signál
- voliteľne odhlásiť ukončené micro-úlohy zo signálu
- hovor
abort
metóda kontrolóra, ak chcete zrušiť všetky vyžiadané micro-úlohy
To je dosť podrobný a zložité riešenie s potenciálne úniky pamäte.
Môžem len navrhnúť vlastné riešenie na tento problém- c-promise2, ktorý poskytuje cancelable sľuby a cancelable alternatíva pre ECMA asynchrónne funkcie - generátory.
Tu je základný príklad (Live Demo):
import { CPromise } from "c-promise2";
// deeply cancelable generator-based asynchronous function
const oneHourLastingFunction = CPromise.promisify(function* () {
// optionally just for logging
this.onCancel(() =>
console.log("oneHourLastingFunction::Cancel signal received")
);
yield CPromise.delay(5000); // this task will be cancelled on external timeout
return "myData";
});
async function nativeAsyncFn() {
await CPromise.delay(5000);
}
async function myFunc() {
let response;
try {
response = await oneHourLastingFunction().timeout(2000);
} catch (err) {
if (!CPromise.isCanceledError(err)) throw err;
console.warn("oneHourLastingFunction::timeout", err.code); // 'E_REASON_TIMEOUT'
}
await nativeAsyncFn(response);
}
const nativePromise = myFunc();
Hlboko cancellable riešenie (všetky funkcie sú cancellable) (Live Demo):
import { CPromise } from "c-promise2";
// deeply cancelable generator-based asynchronous function
const oneHourLastingFunction = CPromise.promisify(function* () {
yield CPromise.delay(5000);
return "myData";
});
const otherAsyncFn = CPromise.promisify(function* () {
yield CPromise.delay(5000);
});
const myFunc = CPromise.promisify(function* () {
let response;
try {
response = yield oneHourLastingFunction().timeout(2000);
} catch (err) {
if (err.code !== "E_REASON_TIMEOUT") throw err;
console.log("oneHourLastingFunction::timeout");
}
yield otherAsyncFn(response);
});
const cancellablePromise = myFunc().then(
(result) => console.log(`Done: ${result}`),
(err) => console.warn(`Failed: ${err}`)
);
setTimeout(() => {
console.log("send external cancel signal");
cancellablePromise.cancel();
}, 4000);