import { Vector } from "./Vector"; import { Option } from "./Option"; import { Either } from "./Either"; /** * A Future is the equivalent, and ultimately wraps, a javascript Promise. * While Futures support the [[Future.then]] call (so that among others * you can use `await` on them), you should call [[Future.map]] and * [[Future.flatMap]]. * * Futures represent an asynchronous computation. A Future will only ever * be computed once at most. Once it's computed, calling [[Future.map]] or * `await` will return instantly. */ export declare class Future { private promise; private constructor(); /** * Build a Future in the same way as the 'new Promise' * constructor. * You get one callback to signal success (resolve), * failure (reject), or you can throw to signal failure. * * Future.ofPromiseCtor((resolve,reject) => setTimeout(resolve, 10, "hello!")) */ static ofPromiseCtor(executor: (resolve: (x: T) => void, reject: (x: any) => void) => void): Future; /** * Build a Future from an existing javascript Promise. */ static of(promise: Promise): Future; /** * Build a Future from a node-style callback API, for instance: * * Future.ofCallback(cb => fs.readFile('/etc/passwd', 'utf-8', cb)) */ static ofCallback(fn: (cb: (err: any, val: T) => void) => void): Future; /** * Build a successful Future with the value you provide. */ static ok(val: T): Future; /** * Build a failed Future with the error data you provide. */ static failed(reason: any): Future; /** * Creates a Future from a function returning a Promise, * which can be inline in the call, for instance: * * const f1 = Future.ok(1); * const f2 = Future.ok(2); * return Future.do(async () => { * const v1 = await f1; * const v2 = await f2; * return v1 + v2; * }); */ static do(fn: () => Promise): Future; /** * The `then` call is not meant to be a part of the `Future` API, * we need then so that `await` works directly. * * Please rather use [[Future.map]] or [[Future.flatMap]]. */ then(onfulfilled: ((value: T) => TResult1 | PromiseLike), onrejected?: ((reason: any) => TResult2 | PromiseLike) | undefined | null): PromiseLike; /** * Get a `Promise` from this `Future`. */ toPromise(): Promise; /** * Returns a `Future` that'll complete when the first `Future` of * the iterable you give will complete, with the value of that first * future. Be careful, completing doesn't necessarily mean completing * successfully! * * Also see [[Future.firstSuccessfulOf]] */ static firstCompletedOf(elts: Iterable>): Future; /** * Returns a `Future` that'll complete when the first `Future` of * the iterable you give will complete successfully, with the value of that first * future. * * Also see [[Future.firstCompletedOf]] */ static firstSuccessfulOf(elts: Iterable>): Future; /** * Turns a list of futures in a future containing a list of items. * Useful in many contexts. * * But if a single future is failed, you get back a failed Future. * * Also see [[Future.traverse]] */ static sequence(elts: Iterable>): Future>; /** * Takes a list, a function that can transform list elements * to futures, then return a Future containing a list of * the transformed elements. * * But if a single element results in failure, the result also * resolves to a failure. * * There is an optional third parameter to specify options. * You can specify `{maxConcurrent: number}` to request that * the futures are not all triggered at the same time, but * rather only 'number' at a time. * * Also see [[Future.sequence]] */ static traverse(elts: Iterable, fn: (x: T) => Future, opts?: { maxConcurrent: number; }): Future>; /** * From the list of Futures you give, will attempt to find a successful * Future which value matches the predicate you give. * We return a Future of an [[Option]], which will [[None]] in case * no matching Future is found. */ static find(elts: Iterable>, p: (x: T) => boolean): Future>; /** * Applicative lifting for Future. 'p' stands for 'properties'. * * Takes a function which operates on a simple JS object, and turns it * in a function that operates on the same JS object type except which each field * wrapped in a Future ('lifts' the function). * It's an alternative to [[Future.liftA2]] when the number of parameters * is not two. * * @param A the object property type specifying the parameters for your function * @param B the type returned by your function, returned wrapped in a future by liftAp. */ static liftAp(fn: (x: A) => B): (x: { [K in keyof A]: Future; }) => Future; /** * Applicative lifting for Future. * Takes a function which operates on basic values, and turns it * in a function that operates on futures of these values ('lifts' * the function). The 2 is because it works on functions taking two * parameters. * * @param R1 the first future type * @param R2 the second future type * @param V the new future type as returned by the combining function. */ static liftA2(fn: (v1: R1, v2: R2) => V): (p1: Future, p2: Future) => Future; /** * Take a function returning a Promise * and lift it to return a [[Future]] instead. */ static lift(fn: (...args: T) => Promise): (...args: T) => Future; /** * Transform the value contained in a successful Future. Has no effect * if the Future was failed. Will turn a successful Future in a failed * one if you throw an exception in the map callback (but please don't * do it.. Rather use [[Future.filter]] or another mechanism). */ map(fn: (x: T) => U): Future; /** * Transform the value contained in a successful Future. You return a * Future, but it is then "flattened" so we still return a Future * (and not a Future>). * Has no effect if the Future was failed. Will turn a successful Future in a failed * one if you throw an exception in the map callback (but please don't * do it.. Rather use [[Future.filter]] or another mechanism). * This is the monadic bind. */ flatMap(fn: (x: T) => Future): Future; /** * Transform the value contained in a failed Future. Has no effect * if the Future was successful. */ mapFailure(fn: (x: any) => any): Future; /** * Execute the side-effecting function you give if the Future is a failure. * * The Future is unchanged by this call. */ onFailure(fn: (x: any) => void): Future; /** * Execute the side-effecting function you give if the Future is a success. * * The Future is unchanged by this call. */ onSuccess(fn: (x: T) => void): Future; /** * Execute the side-effecting function you give when the Future is * completed. You get an [[Either]], a `Right` if the Future is a * success, a `Left` if it's a failure. * * The Future is unchanged by this call. */ onComplete(fn: (x: Either) => void): Future; /** * Has no effect on a failed Future. If the Future was successful, * will check whether its value matches the predicate you give as * first parameter. If the value matches the predicate, an equivalent * Future to the input one is returned. * * If the value doesn't match predicate however, the second parameter * function is used to compute the contents of a failed Future that'll * be returned. */ filter(p: (x: T) => boolean, ifFail: (x: T) => any): Future; /** * Has no effect if this Future is successful. If it's failed however, * the function you give will be called, receiving as parameter * the error contents, and a Future equivalent to the one your * function returns will be returned. */ recoverWith(f: (err: any) => Future): Future; /** * Transform this value to another value type. * Enables fluent-style programming by chaining calls. */ transform(fn: (x: Future) => U): U; }