CancellablePromise
Type Parameters
- T
what the
CancellablePromiseresolves to
Index
Constructors
Properties
Methods
Descriptions can be added for groups with @groupDescription, which will show up in
the index where groups are listed. This works for both manually created groups which
are created with @group, and implicit groups like the Methods group that this
description is attached to.
Constructors
constructor
new CancellablePromise<T>(
promise: PromiseLike<T>,
cancel: (reason?: string) => void,
): CancellablePromise<T>Type Parameters
- T
Parameters
promise: PromiseLike<T>a normal promise or thenable
cancel: (reason?: string) => voida function that cancels
promise. Callingcancelafterpromisehas resolved must be a no-op.
Returns CancellablePromise<T>
Properties
Protected Readonlypromise
As a consumer of the library, you shouldn't ever need to access
CancellablePromise.promise directly.
If you are subclassing CancellablePromise for some reason, you
can access this property.
Readonlycancel
Cancel the CancellablePromise.
Methods
then
then<TResult1 = T, TResult2 = never>(
onFulfilled?: null | ((value: T) => TResult1 | PromiseLike<TResult1>),
onRejected?: null | ((reason: any) => TResult2 | PromiseLike<TResult2>),
): CancellablePromise<TResult1 | TResult2>Analogous to Promise.then.
onFulfilled on onRejected can return a value, a normal promise, or a
CancellablePromise. So you can make a chain a CancellablePromises
like this:
const overallPromise = cancellableAsyncFunction1()
.then(cancellableAsyncFunction2)
.then(cancellableAsyncFunction3)
.then(cancellableAsyncFunction4)
Then if you call overallPromise.cancel, cancel is called on all
CancellablePromises in the chain! In practice, this means that
whichever async operation is in progress will be canceled.
Type Parameters
- TResult1 = T
- TResult2 = never
Parameters
Returns CancellablePromise<TResult1 | TResult2>
a new CancellablePromise
catch
catch<TResult = never>(
onRejected?: null | ((reason: any) => TResult | PromiseLike<TResult>),
): CancellablePromise<T | TResult>finally
finally(onFinally?: null | (() => void)): CancellablePromise<T>Attaches a callback that is invoked when the Promise is settled (fulfilled or rejected). The resolved value cannot be modified from the callback.
Parameters
OptionalonFinally: null | (() => void)The callback to execute when the Promise is settled (fulfilled or rejected).
Returns CancellablePromise<T>
A Promise for the completion of the callback.
Staticresolve
resolve(): CancellablePromise<void>Analogous to Promise.resolve.
The returned promise should resolve even if it is canceled. The idea is that the promise is resolved instantaneously, so by the time the promise is canceled, it has already resolved.
Returns CancellablePromise<void>
resolve<T>(value: T): CancellablePromise<T>Analogous to Promise.resolve.
The returned promise should resolve even if it is canceled. The idea is that the promise is resolved instantaneously, so by the time the promise is canceled, it has already resolved.
Type Parameters
- T
Parameters
value: T
Returns CancellablePromise<T>
Staticreject
reject<T>(reason?: unknown): CancellablePromise<T>Analogous to Promise.reject.
Like CancellablePromise.resolve, canceling the returned
CancellablePromise is a no-op.
Type Parameters
- T
Parameters
Optionalreason: unknownthis should probably be an
Errorobject
Returns CancellablePromise<T>
Staticall
all<T1, T2, T3, T4, T5, T6, T7, T8, T9, T10>(
values: readonly [
T1
| PromiseLike<T1>,
T2 | PromiseLike<T2>,
T3 | PromiseLike<T3>,
T4 | PromiseLike<T4>,
T5 | PromiseLike<T5>,
T6 | PromiseLike<T6>,
T7 | PromiseLike<T7>,
T8 | PromiseLike<T8>,
T9 | PromiseLike<T9>,
T10 | PromiseLike<T10>,
],
): CancellablePromise<[T1, T2, T3, T4, T5, T6, T7, T8, T9, T10]>all<T1, T2, T3, T4, T5, T6, T7, T8, T9>(
values: readonly [
T1
| PromiseLike<T1>,
T2 | PromiseLike<T2>,
T3 | PromiseLike<T3>,
T4 | PromiseLike<T4>,
T5 | PromiseLike<T5>,
T6 | PromiseLike<T6>,
T7 | PromiseLike<T7>,
T8 | PromiseLike<T8>,
T9 | PromiseLike<T9>,
],
): CancellablePromise<[T1, T2, T3, T4, T5, T6, T7, T8, T9]>all<T1, T2, T3, T4, T5, T6, T7, T8>(
values: readonly [
T1
| PromiseLike<T1>,
T2 | PromiseLike<T2>,
T3 | PromiseLike<T3>,
T4 | PromiseLike<T4>,
T5 | PromiseLike<T5>,
T6 | PromiseLike<T6>,
T7 | PromiseLike<T7>,
T8 | PromiseLike<T8>,
],
): CancellablePromise<[T1, T2, T3, T4, T5, T6, T7, T8]>all<T1, T2, T3, T4, T5, T6, T7>(
values: readonly [
T1
| PromiseLike<T1>,
T2 | PromiseLike<T2>,
T3 | PromiseLike<T3>,
T4 | PromiseLike<T4>,
T5 | PromiseLike<T5>,
T6 | PromiseLike<T6>,
T7 | PromiseLike<T7>,
],
): CancellablePromise<[T1, T2, T3, T4, T5, T6, T7]>all<T1, T2, T3, T4, T5, T6>(
values: readonly [
T1
| PromiseLike<T1>,
T2 | PromiseLike<T2>,
T3 | PromiseLike<T3>,
T4 | PromiseLike<T4>,
T5 | PromiseLike<T5>,
T6 | PromiseLike<T6>,
],
): CancellablePromise<[T1, T2, T3, T4, T5, T6]>all<T1, T2, T3, T4, T5>(
values: readonly [
T1
| PromiseLike<T1>,
T2 | PromiseLike<T2>,
T3 | PromiseLike<T3>,
T4 | PromiseLike<T4>,
T5 | PromiseLike<T5>,
],
): CancellablePromise<[T1, T2, T3, T4, T5]>all<T1, T2, T3, T4>(
values: readonly [
T1
| PromiseLike<T1>,
T2 | PromiseLike<T2>,
T3 | PromiseLike<T3>,
T4 | PromiseLike<T4>,
],
): CancellablePromise<[T1, T2, T3, T4]>all<T1, T2, T3>(
values: readonly [
T1
| PromiseLike<T1>,
T2 | PromiseLike<T2>,
T3 | PromiseLike<T3>,
],
): CancellablePromise<[T1, T2, T3]>all<T1, T2>(
values: readonly [T1 | PromiseLike<T1>, T2 | PromiseLike<T2>],
): CancellablePromise<[T1, T2]>all<T>(values: readonly (T | PromiseLike<T>)[]): CancellablePromise<T[]>Staticall Settled
allSettled<T extends readonly unknown[] | readonly [unknown]>(
values: T,
): CancellablePromise<
{
-readonly [P in string
| number
| symbol]: PromiseSettledResult<
T[P<P>] extends PromiseLike<U> ? U : T[P<P>],
>
},
>Creates a CancellablePromise that is resolved with an array of results
when all of the provided Promises resolve or reject.
Type Parameters
- T extends readonly unknown[] | readonly [unknown]
Parameters
values: TAn array of
Promises.
Returns CancellablePromise<
{
-readonly [P in string
| number
| symbol]: PromiseSettledResult<
T[P<P>] extends PromiseLike<U> ? U : T[P<P>],
>
},
>
A new CancellablePromise.
allSettled<T>(
values: Iterable<T>,
): CancellablePromise<
PromiseSettledResult<T extends PromiseLike<U> ? U : T>[],
>Creates a CancellablePromise that is resolved with an array of results
when all of the provided Promises resolve or reject.
Type Parameters
- T
Parameters
values: Iterable<T>An array of
Promises.
Returns CancellablePromise<PromiseSettledResult<T extends PromiseLike<U> ? U : T>[]>
A new CancellablePromise. Canceling it cancels all of the input
promises.
Staticrace
race<T>(values: readonly T[]): CancellablePromise<Awaited<T>>Creates a CancellablePromise that is resolved or rejected when any of
the provided Promises are resolved or rejected.
Type Parameters
- T
Parameters
values: readonly T[]An array of
Promises.
Returns CancellablePromise<Awaited<T>>
A new CancellablePromise. Canceling it cancels all of the input
promises.
Staticdelay
delay(ms: number): CancellablePromise<void>Parameters
ms: number
Returns CancellablePromise<void>
a CancellablePromise that resolves after ms milliseconds.
This example shows off how TypeDoc handles
A promise with a
cancelmethod. If canceled, theCancellablePromisewill reject with aCancellationobject. Originally from real-cancellable-promise.