CancellablePromise
Type Parameters
- T
what the
CancellablePromise
resolves 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) => void
a function that cancels
promise
. Callingcancel
afterpromise
has resolved must be a no-op.
Returns CancellablePromise<T>
Properties
Protected
Readonly
promise
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.
Readonly
cancel
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 CancellablePromise
s
like this:
const overallPromise = cancellableAsyncFunction1()
.then(cancellableAsyncFunction2)
.then(cancellableAsyncFunction3)
.then(cancellableAsyncFunction4)
Then if you call overallPromise.cancel
, cancel
is called on all
CancellablePromise
s 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
Optional
onFinally: 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.
Static
resolve
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>
Static
reject
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
Optional
reason: unknownthis should probably be an
Error
object
Returns CancellablePromise<T>
Static
all
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[]>
Static
all 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: T
An 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 Promise
s resolve or reject.
Type Parameters
- T
Parameters
values: Iterable<T>
An array of
Promise
s.
Returns CancellablePromise<PromiseSettledResult<T extends PromiseLike<U> ? U : T>[]>
A new CancellablePromise
. Canceling it cancels all of the input
promises.
Static
race
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.
Static
delay
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
cancel
method. If canceled, theCancellablePromise
will reject with aCancellation
object. Originally from real-cancellable-promise.