Options
All
  • Public
  • Public/Protected
  • All
Menu

Class Future<T>

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.

Type parameters

  • T

Hierarchy

  • Future

Index

Methods

Static do

  • do<T>(fn: function): Future<T>
  • 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;
    });
    

    Type parameters

    • T

    Parameters

    • fn: function
        • (): Promise<T>
        • Returns Promise<T>

    Returns Future<T>

Static failed

  • failed<T>(reason: any): Future<T>
  • Build a failed Future with the error data you provide.

    Type parameters

    • T

    Parameters

    • reason: any

    Returns Future<T>

Static find

  • 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.

    Type parameters

    • T

    Parameters

    • elts: Iterable<Future<T>>
    • p: function
        • (x: T): boolean
        • Parameters

          • x: T

          Returns boolean

    Returns Future<Option<T>>

Static firstCompletedOf

  • 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

    Type parameters

    • T

    Parameters

    Returns Future<T>

Static firstSuccessfulOf

  • firstSuccessfulOf<T>(elts: Iterable<Future<T>>): Future<T>
  • 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

    Type parameters

    • T

    Parameters

    Returns Future<T>

Static lift

  • lift<T, U>(fn: function): function
  • Take a function returning a Promise and lift it to return a Future instead.

    Type parameters

    • T: any[]

    • U

    Parameters

    • fn: function
        • (...args: T): Promise<U>
        • Parameters

          • Rest ...args: T

          Returns Promise<U>

    Returns function

      • Parameters

        • Rest ...args: T

        Returns Future<U>

Static liftA2

  • liftA2<R1, R2, V>(fn: function): function
  • 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.

    Type parameters

    • R1

      the first future type

    • R2

      the second future type

    • V

      the new future type as returned by the combining function.

    Parameters

    • fn: function
        • (v1: R1, v2: R2): V
        • Parameters

          • v1: R1
          • v2: R2

          Returns V

    Returns function

Static liftAp

  • liftAp<A, B>(fn: function): function
  • 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.

    Type parameters

    • A

      the object property type specifying the parameters for your function

    • B

      the type returned by your function, returned wrapped in a future by liftAp.

    Parameters

    • fn: function
        • (x: A): B
        • Parameters

          • x: A

          Returns B

    Returns function

      • Parameters

        • x: object

        Returns Future<B>

Static of

  • of<T>(promise: Promise<T>): Future<T>
  • Build a Future from an existing javascript Promise.

    Type parameters

    • T

    Parameters

    • promise: Promise<T>

    Returns Future<T>

Static ofCallback

  • ofCallback<T>(fn: function): Future<T>
  • Build a Future from a node-style callback API, for instance:

    Future.ofCallback<string>(cb => fs.readFile('/etc/passwd', 'utf-8', cb))
    

    Type parameters

    • T

    Parameters

    • fn: function
        • (cb: function): void
        • Parameters

          • cb: function
              • (err: any, val: T): void
              • Parameters

                • err: any
                • val: T

                Returns void

          Returns void

    Returns Future<T>

Static ofPromiseCtor

  • ofPromiseCtor<T>(executor: function): Future<T>
  • 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<string>((resolve,reject) => setTimeout(resolve, 10, "hello!"))
    

    Type parameters

    • T

    Parameters

    • executor: function
        • (resolve: function, reject: function): void
        • Parameters

          • resolve: function
              • (x: T): void
              • Parameters

                • x: T

                Returns void

          • reject: function
              • (x: any): void
              • Parameters

                • x: any

                Returns void

          Returns void

    Returns Future<T>

Static ok

  • Build a successful Future with the value you provide.

    Type parameters

    • T

    Parameters

    • val: T

    Returns Future<T>

Static sequence

  • 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

    Type parameters

    • T

    Parameters

    Returns Future<Vector<T>>

Static traverse

  • traverse<T, U>(elts: Iterable<T>, fn: function, opts?: undefined | object): Future<Vector<U>>
  • 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

    Type parameters

    • T

    • U

    Parameters

    • elts: Iterable<T>
    • fn: function
    • Optional opts: undefined | object

    Returns Future<Vector<U>>

filter

  • filter(p: function, ifFail: function): Future<T>
  • 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.

    Parameters

    • p: function
        • (x: T): boolean
        • Parameters

          • x: T

          Returns boolean

    • ifFail: function
        • (x: T): any
        • Parameters

          • x: T

          Returns any

    Returns Future<T>

flatMap

  • flatMap<U>(fn: function): Future<U>
  • 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<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.

    Type parameters

    • U

    Parameters

    • fn: function

    Returns Future<U>

map

  • map<U>(fn: function): Future<U>
  • 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).

    Type parameters

    • U

    Parameters

    • fn: function
        • (x: T): U
        • Parameters

          • x: T

          Returns U

    Returns Future<U>

mapFailure

  • mapFailure(fn: function): Future<T>
  • Transform the value contained in a failed Future. Has no effect if the Future was successful.

    Parameters

    • fn: function
        • (x: any): any
        • Parameters

          • x: any

          Returns any

    Returns Future<T>

onComplete

  • onComplete(fn: function): Future<T>
  • 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.

    Parameters

    • fn: function
        • Parameters

          Returns void

    Returns Future<T>

onFailure

  • onFailure(fn: function): Future<T>
  • Execute the side-effecting function you give if the Future is a failure.

    The Future is unchanged by this call.

    Parameters

    • fn: function
        • (x: any): void
        • Parameters

          • x: any

          Returns void

    Returns Future<T>

onSuccess

  • onSuccess(fn: function): Future<T>
  • Execute the side-effecting function you give if the Future is a success.

    The Future is unchanged by this call.

    Parameters

    • fn: function
        • (x: T): void
        • Parameters

          • x: T

          Returns void

    Returns Future<T>

recoverWith

  • recoverWith(f: function): Future<T>
  • 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.

    Parameters

    • f: function
        • Parameters

          • err: any

          Returns Future<T>

    Returns Future<T>

then

  • then<TResult1, TResult2>(onfulfilled: function, onrejected?: function | undefined | null): PromiseLike<TResult1 | TResult2>
  • 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.

    Type parameters

    • TResult1

    • TResult2

    Parameters

    • onfulfilled: function
        • (value: T): TResult1 | PromiseLike<TResult1>
        • Parameters

          • value: T

          Returns TResult1 | PromiseLike<TResult1>

    • Optional onrejected: function | undefined | null

    Returns PromiseLike<TResult1 | TResult2>

toPromise

  • toPromise(): Promise<T>
  • Get a Promise from this Future.

    Returns Promise<T>

transform

  • transform<U>(fn: function): U
  • Transform this value to another value type. Enables fluent-style programming by chaining calls.

    Type parameters

    • U

    Parameters

    • fn: function

    Returns U

Generated using TypeDoc