import { Transform, AsyncMemberNP, PromiseArray, } from '../functions/asyncFlatMap'; export interface FutureOptions { /** Should the executor only be called when this Future is awaited? */ lazy?: boolean; /** how often .poll() is called */ interval?: number; /** @returns a value which resolves this Future, or undefined */ poll?: () => undefined | PromiseLike; timeout?: number; } export declare const TIMEOUT: unique symbol; export type Executor = ( resolve: (value: T | PromiseLike) => void, reject: (reason?: any) => void ) => any; export declare const VoidExecutor: Executor; export type ErrorHandler = (_error: unknown) => any; export type FutureCallback = (_value: Awaited) => any; export type Then = (_cb: FutureCallback, _err: ErrorHandler) => any; export type Thennable = { then: Then; }; export declare class Future implements Promise { value: Awaited | undefined; reason: any; resolved: boolean; rejected: boolean; callbacks: Set<(self: this) => any>; then( onfulfilled?: | ((value: Awaited) => TResult1 | PromiseLike) | null | undefined, onrejected?: | ((reason: any) => TResult2 | PromiseLike) | null | undefined ): Future; catch( onrejected?: | ((reason: any) => TResult | PromiseLike) | null | undefined ): Future | TResult>; finally( onfinally?: ((value?: T) => void | Promise) | null | undefined ): Future; await( executor_or_promise: Executor | PromiseLike | null | undefined ): Future; asyncFlatMap( transform: Transform>> ): Promise>[]>; [Symbol.toStringTag]: string; constructor( executor_or_promise: Executor | PromiseLike, options?: FutureOptions ); protected init_with_executor(executor: Executor): Promise; protected init_with_promise(promise: PromiseLike): Promise; flush(): void; protected resolve(value: T | PromiseLike): Promise; protected reject(reason: any): void; static resolve(value: T | PromiseLike): Future; static reject(reason: any): Future; static all(...items: Array>): Future; static race(items: Array>): Future; /** calls a callback with an awaited value */ static callback( value: T, callback?: FutureCallback, errorHandler?: ErrorHandler ): void; /** calls a callback after awaiting a Future */ static callback( value: Future, callback?: FutureCallback, errorHandler?: ErrorHandler ): void; /** calls a callback after awaiting a Promise */ static callback( value: Promise, callback?: FutureCallback, errorHandler?: ErrorHandler ): void; /** * Transforms a callback consumer call into a `Future`. * # Usage * ```js * function myFunction(callback) { * if (condition) { * callback(null, "Success") * } else { * callback("Error") * } * } * * const future = Future.fromCallback(myFunction) * ``` * Many [Node APIs](https://docs.nodejs.org/) and [old npm packages](https://npmjs.com/package/imap) behave like this. */ static fromCallback( callback: (callback: (err: E, val: T) => void) => void ): Future; /** * Transforms a callback consumer call into a `Future`. * # Usage * ```js * const myObject = { * condition: sky.color === blue, * myMethod(callback) { * if (this.condition) { * callback(null, "Success") * } else { * callback("Error") * } * } * } * * const future = Future.fromCallback(myObject, myObject.myMethod) * ``` * Many [Node APIs](https://docs.nodejs.org/) and [old npm packages](https://npmjs.com/package/imap) behave like this. */ static fromCallback( thisArg: object, callback: (callback: (err: E, val: T) => void) => void ): Future; _interval?: number; _poll?: FutureOptions['poll']; _poll_timeout?: ReturnType; /** * calls this Future's poll callback * @returns a Future that resolves to this Future, or undefined */ poll(): Future; schedule_poll(): void; } export declare class LazyFuture extends Future { protected init: () => Future; callbacks: Set<(self: Future) => any>; constructor( executor_or_promise: Executor | PromiseLike, options?: FutureOptions ); then( onfulfilled?: | ((value: Awaited) => TResult1 | PromiseLike) | null | undefined, onrejected?: | ((reason: any) => TResult2 | PromiseLike) | null | undefined ): Future; /** * Transforms a callback consumer call into a `LazyFuture`. * # Usage * ```js * function myFunction(callback) { * if (condition) { * callback(null, "Success") * } else { * callback("Error") * } * } * * const future = LazyFuture.fromCallback(myFunction) * ``` * Many [Node APIs](https://docs.nodejs.org/) and [old npm packages](https://npmjs.com/package/imap) behave like this. */ static fromCallback( callback: (callback: (err: E, val: T) => void) => void ): LazyFuture; /** * Transforms a callback consumer call into a `LazyFuture`. * # Usage * ```js * const myObject = { * condition: sky.color === blue, * myMethod(callback) { * if (this.condition) { * callback(null, "Success") * } else { * callback("Error") * } * } * } * * const future = LazyFuture.fromCallback(myObject, myObject.myMethod) * ``` * Many [Node APIs](https://docs.nodejs.org/) and [old npm packages](https://npmjs.com/package/imap) behave like this. */ static fromCallback( thisArg: object, callback: (callback: (err: E, val: T) => void) => void ): LazyFuture; } export default Future;