UNPKG

2.59 kBTypeScriptView Raw
1/**
2 * @template V
3 * @typedef {V | PledgeInstance<V>} Pledge
4 */
5/**
6 * @template {any} Val
7 * @template {any} [CancelReason=Error]
8 */
9export class PledgeInstance<Val extends unknown, CancelReason extends unknown = Error> {
10 /**
11 * @type {Val | CancelReason | null}
12 */
13 _v: Val | CancelReason | null;
14 isResolved: boolean;
15 /**
16 * @type {Array<function(Val):void> | null}
17 */
18 _whenResolved: ((arg0: Val) => void)[] | null;
19 /**
20 * @type {Array<function(CancelReason):void> | null}
21 */
22 _whenCanceled: ((arg0: CancelReason) => void)[] | null;
23 get isDone(): boolean;
24 get isCanceled(): boolean;
25 /**
26 * @param {Val} v
27 */
28 resolve(v: Val): void;
29 /**
30 * @param {CancelReason} reason
31 */
32 cancel(reason: CancelReason): void;
33 /**
34 * @template R
35 * @param {function(Val):Pledge<R>} f
36 * @return {PledgeInstance<R>}
37 */
38 map<R>(f: (arg0: Val) => Pledge<R>): PledgeInstance<R, Error>;
39 /**
40 * @param {function(Val):void} f
41 */
42 whenResolved(f: (arg0: Val) => void): void;
43 /**
44 * @param {(reason: CancelReason) => void} f
45 */
46 whenCanceled(f: (reason: CancelReason) => void): void;
47 /**
48 * @return {Promise<Val>}
49 */
50 promise(): Promise<Val>;
51}
52export function create<T>(): PledgeInstance<T, Error>;
53export function createWithDependencies<V, DEPS extends unknown[]>(init: (p: PledgeInstance<V, Error>, ...deps: Resolved<DEPS>) => void, ...deps: DEPS): PledgeInstance<V, Error>;
54export function whenResolved<R>(p: Pledge<R>, f: (arg0: R) => void): void;
55export function whenCanceled<P extends unknown>(p: P, f: P extends PledgeInstance<unknown, infer CancelReason extends unknown> ? (arg0: CancelReason) => void : (arg0: any) => void): void;
56export function map<P, Q>(p: Pledge<P>, f: (r: P) => Q): Pledge<Q>;
57export function all<PS extends PledgeMap>(ps: PS): PledgeInstance<Resolved<PS>, Error>;
58export function coroutine<Result, YieldResults extends unknown>(f: () => Generator<Pledge<YieldResults> | PledgeInstance<YieldResults, any>, Result, any>): PledgeInstance<Result, Error>;
59export function wait(timeout: number): PledgeInstance<undefined>;
60export type Pledge<V> = V | PledgeInstance<V>;
61export type PledgeMap = Array<Pledge<unknown>> | {
62 [x: string]: Pledge<unknown>;
63};
64/**
65 * <P>
66 */
67export type Resolved<P extends unknown> = P extends PledgeMap ? P extends infer T extends PledgeMap ? { [K in keyof T]: P[K] extends Pledge<infer V> ? V : P[K]; } : never : P extends Pledge<infer V_1> ? V_1 : never;
68//# sourceMappingURL=pledge.d.ts.map
\No newline at end of file