index.d.ts 4.46 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168
/**
 * Accepts a function that is called when the promise is canceled.
 *
 * You're not required to call this function. You can call this function multiple times to add multiple cancel handlers.
 */
export interface OnCancelFunction {
	(cancelHandler: () => void): void;
	shouldReject: boolean;
}

declare class PCancelable<ValueType> extends Promise<ValueType> {
	/**
	 * Convenience method to make your promise-returning or async function cancelable.
	 *
	 * @param fn - A promise-returning function. The function you specify will have `onCancel` appended to its parameters.
	 *
	 * @example
	 *
	 * import PCancelable from 'p-cancelable';
	 *
	 * const fn = PCancelable.fn((input, onCancel) => {
	 * 	const job = new Job();
	 *
	 * 	onCancel(() => {
	 * 		job.cleanup();
	 * 	});
	 *
	 * 	return job.start(); //=> Promise
	 * });
	 *
	 * const cancelablePromise = fn('input'); //=> PCancelable
	 *
	 * // …
	 *
	 * cancelablePromise.cancel();
	 */
	static fn<ReturnType>(
		userFn: (onCancel: OnCancelFunction) => PromiseLike<ReturnType>
	): () => PCancelable<ReturnType>;
	static fn<Agument1Type, ReturnType>(
		userFn: (
			argument1: Agument1Type,
			onCancel: OnCancelFunction
		) => PromiseLike<ReturnType>
	): (argument1: Agument1Type) => PCancelable<ReturnType>;
	static fn<Agument1Type, Agument2Type, ReturnType>(
		userFn: (
			argument1: Agument1Type,
			argument2: Agument2Type,
			onCancel: OnCancelFunction
		) => PromiseLike<ReturnType>
	): (
		argument1: Agument1Type,
		argument2: Agument2Type
	) => PCancelable<ReturnType>;
	static fn<Agument1Type, Agument2Type, Agument3Type, ReturnType>(
		userFn: (
			argument1: Agument1Type,
			argument2: Agument2Type,
			argument3: Agument3Type,
			onCancel: OnCancelFunction
		) => PromiseLike<ReturnType>
	): (
		argument1: Agument1Type,
		argument2: Agument2Type,
		argument3: Agument3Type
	) => PCancelable<ReturnType>;
	static fn<Agument1Type, Agument2Type, Agument3Type, Agument4Type, ReturnType>(
		userFn: (
			argument1: Agument1Type,
			argument2: Agument2Type,
			argument3: Agument3Type,
			argument4: Agument4Type,
			onCancel: OnCancelFunction
		) => PromiseLike<ReturnType>
	): (
		argument1: Agument1Type,
		argument2: Agument2Type,
		argument3: Agument3Type,
		argument4: Agument4Type
	) => PCancelable<ReturnType>;
	static fn<
		Agument1Type,
		Agument2Type,
		Agument3Type,
		Agument4Type,
		Agument5Type,
		ReturnType
	>(
		userFn: (
			argument1: Agument1Type,
			argument2: Agument2Type,
			argument3: Agument3Type,
			argument4: Agument4Type,
			argument5: Agument5Type,
			onCancel: OnCancelFunction
		) => PromiseLike<ReturnType>
	): (
		argument1: Agument1Type,
		argument2: Agument2Type,
		argument3: Agument3Type,
		argument4: Agument4Type,
		argument5: Agument5Type
	) => PCancelable<ReturnType>;
	static fn<ReturnType>(
		userFn: (...arguments: unknown[]) => PromiseLike<ReturnType>
	): (...arguments: unknown[]) => PCancelable<ReturnType>;

	/**
	 * Create a promise that can be canceled.
	 *
	 * Can be constructed in the same was as a [`Promise` constructor](https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/Promise), but with an appended `onCancel` parameter in `executor`. `PCancelable` is a subclass of `Promise`.
	 *
	 * Cancelling will reject the promise with `CancelError`. To avoid that, set `onCancel.shouldReject` to `false`.
	 *
	 * @example
	 *
	 * import PCancelable from 'p-cancelable';
	 *
	 * const cancelablePromise = new PCancelable((resolve, reject, onCancel) => {
	 * 	const job = new Job();
	 *
	 * 	onCancel.shouldReject = false;
	 * 	onCancel(() => {
	 * 		job.stop();
	 * 	});
	 *
	 * 	job.on('finish', resolve);
	 * });
	 *
	 * cancelablePromise.cancel(); // Doesn't throw an error
	 */
	constructor(
		executor: (
			resolve: (value?: ValueType | PromiseLike<ValueType>) => void,
			reject: (reason?: unknown) => void,
			onCancel: OnCancelFunction
		) => void
	);

	/**
	 * Whether the promise is canceled.
	 */
	readonly isCanceled: boolean;

	/**
	 * Cancel the promise and optionally provide a reason.
	 *
	 * The cancellation is synchronous. Calling it after the promise has settled or multiple times does nothing.
	 *
	 * @param reason - The cancellation reason to reject the promise with.
	 */
	cancel(reason?: string): void;
}

export default PCancelable;

/**
 * Rejection reason when `.cancel()` is called.
 *
 * It includes a `.isCanceled` property for convenience.
 */
export class CancelError extends Error {
	readonly name: 'CancelError';
	readonly isCanceled: true;

	constructor(reason?: string);
}