UNPKG

ahooks

Version:
32 lines (31 loc) 1.34 kB
var _a; const CANCELLED_ERROR_FLAG = '__AHOOKS_CANCELLED_ERROR__'; /** * Rejection reason used when `useRequest` ignores a request, either because * `cancel()` was called (or the component unmounted), or because a newer call superseded it. * * It is swallowed by `run`/`refresh`, by `options.onError` and by the plugin `onError` * handlers, so it only surfaces to code that awaits `runAsync`/`refreshAsync` directly. */ export class CancelledError extends Error { constructor(message = 'useRequest: the request was cancelled or superseded.') { super(message); /** Marker that survives duplicated copies of the module, unlike `instanceof`. */ this[_a] = true; this.name = 'CancelledError'; // keep `instanceof` working when the class is downleveled Object.setPrototypeOf(this, CancelledError.prototype); } } _a = CANCELLED_ERROR_FLAG; export function isCancelledError(error) { return (error instanceof CancelledError || (typeof error === 'object' && error !== null && error[CANCELLED_ERROR_FLAG] === true)); } export function cancelPendingPromise(pendingRejectRef) { var _b; (_b = pendingRejectRef.current) === null || _b === void 0 ? void 0 : _b.call(pendingRejectRef, new CancelledError()); pendingRejectRef.current = undefined; }