import type { CancelCallback, CancelablePromiseCallback, OnProgressCallback, PromiseStatus, SubscriptionParams } from './types'; import { promise_identifier } from './isCancelablePromise'; /** * A Promise that can be canceled with lifecycle and progress tracking. * * CancelablePromise extends the native Promise with additional capabilities: * - Cancellation support via `cancel()` method * - Status tracking: 'pending', 'resolved', 'rejected', or 'canceled' * - Progress reporting through `reportProgress()` and `onProgress()` * - Event subscription for cancellation via `onCancel()` * * @template TResult - The type of value the promise resolves to * * @example * ```ts * const promise = new CancelablePromise((resolve, reject, { onCancel, reportProgress }) => { * const timeoutId = setTimeout(() => resolve('Done!'), 5000); * * onCancel(() => { * clearTimeout(timeoutId); * console.log('Canceled!'); * }); * * // Report progress * reportProgress(50); * }); * * promise * .then(console.log) * .onProgress((progress) => console.log(`${progress}% complete`)) * .onCancel((reason) => console.log('Canceled:', reason)); * * // Cancel after 1 second * setTimeout(() => promise.cancel('User canceled'), 1000); * ``` * * @example * ```ts * // Canceling from within the executor * const promise = new CancelablePromise((resolve, reject, { cancel }) => { * // Some condition triggers cancellation * if (shouldCancel) { * cancel('Canceled internally'); * return; * } * resolve('Success'); * }); * ``` */ export declare class CancelablePromise extends Promise { protected [promise_identifier]: boolean; /** * The current status of the promise. */ status: PromiseStatus; private cancelCallbacks; private ownCancelCallbacks; private onProgressCallbacks; private disposeCallbacks; private _resolve; private _reject; constructor(callback: CancelablePromiseCallback); private subscribeToOwnCancelEvent; /** * Cancel the promise and all the chained promises. * @param {unknown} [reason] the reason of the cancellation * @returns {CancelablePromise} the promise itself * */ cancel(reason?: unknown): CancelablePromise; /** * Subscribe to the cancel event of the promise. * @param {CancelCallback} [callback] the callback to be called when the promise is canceled * @returns {CancelablePromise} the promise itself * */ onCancel(callback: CancelCallback, { signal }?: SubscriptionParams): CancelablePromise; /** * This method allows to report the progress across the chain of promises. * */ onProgress(callback: OnProgressCallback, { signal }?: SubscriptionParams): CancelablePromise; /** * This allows to report progress across the chain of promises, * this is useful when you have an async operation that could take a long time and you want to report the progress to the user. */ reportProgress(percentage: number, metadata?: unknown): this; private createChildPromise; /** * Returns a Promise that resolves or rejects as soon as the previous promise is resolved or rejected, * with cancelable promise you can call the cancel method on the child promise to cancel all the parent promises. * @param {((value: TResult) => TResult1 | PromiseLike) | undefined | null} [onfulfilled] the callback to be called when the promise is resolved * @param {((reason: unknown) => TResult2 | PromiseLike) | undefined | null} [onrejected] the callback to be called when the promise is rejected * @returns {CancelablePromise} the cancelable promise * @example * const promise = new CancelablePromise((resolve, reject, utils) => { * setTimeout(() => { * resolve('resolved'); * }, 1000); * }); * * const childPromise = promise.then((value) => { * console.log(value); // 'resolved' * }); * * childPromise.cancel(); * console.log(childPromise.status); // 'canceled' * console.log(promise.status); // 'canceled' */ then: (onfulfilled?: ((value: TResult) => TResult1 | PromiseLike) | undefined | null, onrejected?: ((reason: unknown) => TResult2 | PromiseLike) | undefined | null) => CancelablePromise; /** * Returns a Promise that resolves when the previous promise is rejected, * with cancelable promise you can call the cancel method on the child promise to cancel all the parent promises. * @param {((reason: any) => T | PromiseLike) | undefined | null} [onrejected] the callback to be called when the promise is rejected * @returns {CancelablePromise} the cancelable promise * @example * const promise = new CancelablePromise((resolve, reject, utils) => { * setTimeout(() => { * reject('rejected'); * }, 1000); * }); * * const childPromise = promise.catch(() => { * console.log(childPromise.status); // 'canceled' * console.log(promise.status); // 'canceled' * }); * * childPromise.cancel(); * */ catch: (onrejected?: (reason: unknown) => T | PromiseLike) => CancelablePromise; /** * Subscribe a callback to be called when the promise is resolved or rejected, */ finally: (onfinally?: (() => void) | undefined | null) => CancelablePromise; /** * Statics */ static resolve(): CancelablePromise; static resolve(value: TResult): CancelablePromise>; static resolve(value: TResult | PromiseLike): CancelablePromise>; static reject: (reason?: unknown) => CancelablePromise; /** * Returns a Promise object with status 'canceled'. */ static canceled: (reason?: unknown) => CancelablePromise; /** * Creates a Promise that is resolved or rejected when any of the provided Promises are resolved or rejected. * If the race is canceled, all the promises are canceled. */ static race: (values: (TResult_1 | PromiseLike | CancelablePromise)[]) => CancelablePromise>; /** * Creates a Promise that is resolved with an array of results when all of the provided Promises resolve, or rejected when any Promise is rejected. * If the all is canceled, all the promises are canceled. */ static all: (values: TSource) => CancelablePromise<{ -readonly [P in keyof TSource]: Awaited; }>; /** * Creates a Promise that is resolved with an array of results when all * of the provided Promises resolve or reject. * If the allSettled is canceled, all the promises are canceled. * @param values An array of Promises. * @returns A new Promise. */ static allSettled: (values: TResult_1) => CancelablePromise<{ -readonly [P in keyof TResult_1]: PromiseSettledResult>; }>; } /** * Convert a value to a CancelablePromise, the value can be a Promise/CancellablePromise or a value. * @param {unknown} source the value to convert * @returns {TCancelablePromise} the CancelablePromise * @example * const promise = new Promise((resolve) => { * setTimeout(() => { * resolve('hello world'); * }, 1000); * }); * const cancelablePromise = toCancelablePromise(promise); * cancelablePromise.onCancel(() => { * console.log('promise canceled'); * }); * cancelablePromise.cancel(); * // promise canceled * */ export declare const toCancelablePromise: ? CancelablePromise> : CancelablePromise>(source: T) => CancelablePromise; export default CancelablePromise;