import { RunLoop } from './run-loop'; export type State = 'pending' | 'errored' | 'completed' | 'halted'; export type Value = { state: 'errored'; error: Error; } | { state: 'completed'; value: T; } | { state: 'halted'; }; export interface Consumer { (value: Value): void; } /** * `FutureLike` is a slimmed down interface, similar to `PromiseLink` which * does not make equally strong requirements on the implementor. In Effection, * {@link Task} implements `FutureLike`, but not {@linkcode Future}. * * See [the Futures guide](https://frontside.com/effection/docs/guides/futures) * for a more detailed description of futures and how they work. */ export interface FutureLike { consume(consumer: Consumer): void; } /** * A Future represents a value which may or may not be available yet. It is * similar to a JavaScript Promise, except that it can resolve synchronously. * * See [the Futures guide](https://frontside.com/effection/docs/guides/futures) * for a more detailed description of futures and how they work. * * A Future may resolve to *three* different states. A Future can either become * `completed` with a value, it can become `errored` with an Error or it can * become `halted`, meaning it was prematurely cancelled. * * A Future can be created via the {@link createFuture} function, or via the * The `resolve`, `reject` and `halt` functions on {@link Future}. * * See also the slimmed down {@link FutureLike} interface. */ export interface Future extends Promise, FutureLike { state: State; } export interface NewFuture { future: Future; produce(value: Value): void; resolve(value: T): void; reject(error: Error): void; halt(): void; } /** * Create a new Future. This returns an object which contains the future itself * as well as a function to produce a value for the future, and also shortcuts * to resolve/reject/halt the future. * * ### Example * * ```typescript * import { createFuture } from 'effection'; * * let { future, produce } = createFuture(); * * // later... * produce({ state: 'completed', value: 100 }); * * future.consume((value) => console.log(value)) // => { state: 'completed', value: 100 } * ``` * * ### Example using shortcut * * ```typescript * import { createFuture } from 'effection'; * * let { future, resolve } = createFuture(); * * // later... * resolve(100); * * future.consume((value) => console.log(value)) // => { state: 'completed', value: 100 } * ``` */ export declare function createFuture(): NewFuture; export declare function createFutureOnRunLoop(runLoop: RunLoop): NewFuture; /** * Namespace for `Future` functions to create resolved futures. * * See [Future](./interfaces/Future.html). */ export declare const Future: { /** * Create a future which has resolved successfully to a `completed` state. * * @param value The value to resolve to * @typeParam T The type that the future resolves to */ resolve(value: T): Future; /** * Create a future which has been rejected and is in an `errored` state. * * @param error The error that the future rejects with * @typeParam T The type that the future resolves to */ reject(error: Error): Future; /** * Create a future which has been halted and is in a `halted` state. * * @typeParam T The type that the future resolves to */ halt(): Future; }; //# sourceMappingURL=future.d.ts.map