Skip to main content
Using Deno in production at your company? Earn free Deno merch.
Give us feedback
Module

x/grm/deps.ts>CancellablePromise

MTProto API Client for Deno 🦕
Go to Latest
class CancellablePromise
Re-export
import { CancellablePromise } from "https://deno.land/x/grm@0.8.0/deps.ts";

A promise with a cancel method.

If canceled, the CancellablePromise will reject with a [[Cancellation]] object.

Constructors

new
CancellablePromise(promise: PromiseLike<T>, cancel: (reason?: string) => void)

Properties

protected
readonly
promise: Promise<T>

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: (reason?: string) => void

Cancel the CancellablePromise.

readonly
[Symbol.toStringTag]: string

This is necessary to make CancellablePromise assignable to Promise.

Methods

catch<TResult = never>(onRejected?: ((reason: any) => TResult | PromiseLike<TResult>) | undefined | null): CancellablePromise<T | TResult>

Analogous to Promise.catch.

finally(onFinally?: (() => void) | undefined | null): 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.

then<TResult1 = T, TResult2 = never>(onFulfilled?: ((value: T) => TResult1 | PromiseLike<TResult1>) | undefined | null, onRejected?: ((reason: any) => TResult2 | PromiseLike<TResult2>) | undefined | null): 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.

Static Methods

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,
]
>

Analogous to Promise.all.

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,
]
>

Analogous to Promise.all.

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,
]
>

Analogous to Promise.all.

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,
]
>

Analogous to Promise.all.

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,
]
>

Analogous to Promise.all.

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,
]
>

Analogous to Promise.all.

all<T1, T2, T3, T4>(values: readonly
[
T1 | PromiseLike<T1>,
T2 | PromiseLike<T2>,
T3 | PromiseLike<T3>,
T4 | PromiseLike<T4>,
]
): CancellablePromise<
[
T1,
T2,
T3,
T4,
]
>

Analogous to Promise.all.

all<T1, T2, T3>(values: readonly [T1 | PromiseLike<T1>, T2 | PromiseLike<T2>, T3 | PromiseLike<T3>]): CancellablePromise<[T1, T2, T3]>

Analogous to Promise.all.

all<T1, T2>(values: readonly [T1 | PromiseLike<T1>, T2 | PromiseLike<T2>]): CancellablePromise<[T1, T2]>

Analogous to Promise.all.

all<T>(values: readonly (T | PromiseLike<T>)[]): CancellablePromise<T[]>

Analogous to Promise.all.

allSettled<T extends readonly unknown[] | readonly [unknown]>(values: T): CancellablePromise<-readonly [P in keyof T]: PromiseSettledResult<T[P] extends PromiseLike<infer U> ? U : T[P]>>

Creates a CancellablePromise that is resolved with an array of results when all of the provided Promises resolve or reject.

allSettled<T>(values: Iterable<T>): CancellablePromise<PromiseSettledResult<T extends PromiseLike<infer U> ? U : T>[]>

Creates a CancellablePromise that is resolved with an array of results when all of the provided Promises resolve or reject.

delay(ms: number): CancellablePromise<void>
race<T>(values: readonly T[]): CancellablePromise<T extends PromiseLike<infer U> ? U : T>

Creates a CancellablePromise that is resolved or rejected when any of the provided Promises are resolved or rejected.

reject<T>(reason?: unknown): CancellablePromise<T>

Analogous to Promise.reject.

Like CancellablePromise.resolve, canceling the returned CancellablePromise is a no-op.

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.