Options
All
  • Public
  • Public/Protected
  • All
Menu

@lxsmnsyc/future

Index

Core Type aliases

FutureTransformer

FutureTransformer<T, R>: Function<Future<T>, Future<R>>

A type annotation for a function that transforms a Future instance into another Future instance.

Used for Future.compose and Future.pipe.

Type parameters

  • T

    type for the computed value of the given Future.

  • R

    type for the computed value of the resulting Future.

Scheduler

Scheduler: Function<Action, Computation<unknown>>

Schedules an Action and executes it thereafter.

TimedScheduler

TimedScheduler: Function2<Action, number, Computation<unknown>>

Schedules an Action with a given time delay and executes it thereafter.

Other Type aliases

AsyncFunction

AsyncFunction<T>: Function2<OnCancel, PromiseProtect<T>, Promise<T>>

Type parameters

  • T

OnCancel

OnCancel: Consumer<Action>

PromiseProtect

PromiseProtect<T>: Function<PromiseLike<T> | Computation<T>, Promise<T> | Computation<T>>

Type parameters

  • T

Utility Type aliases

Action

Action: function

Type declaration

    • (): void
    • Returns void

Consumer

Consumer<T>: Function<T, void>

Type parameters

  • T

Consumer2

Consumer2<T1, T2>: Function2<T1, T2, void>

Type parameters

  • T1

  • T2

Function

Function<T, R>: function

Type parameters

  • T

  • R

Type declaration

    • (value: T): R
    • Parameters

      • value: T

      Returns R

Function2

Function2<T1, T2, R>: function

Type parameters

  • T1

  • T2

  • R

Type declaration

    • (v1: T1, v2: T2): R
    • Parameters

      • v1: T1
      • v2: T2

      Returns R

Predicate

Predicate<T>: Function<T, boolean>

Type parameters

  • T

Predicate2

Predicate2<T1, T2>: Function2<T1, T2, boolean>

Type parameters

  • T1

  • T2

Supplier

Supplier<T>: function

Type parameters

  • T

Type declaration

    • (): T
    • Returns T

Constructors Functions

amb

  • amb<T>(futures: T): Future<any>
  • Resolves the Computation instance to the fastest given Future.

    Example below resolves the amb instance to B as it its faster than A.

    const A = Future.timer('Hello', 500);
    const B = Future.timer('World', 400);
    
    Future.amb([A, B])
     .get()
     .then(console.log); // World

    Type parameters

    Parameters

    • futures: T

      an Array of Future instances

    Returns Future<any>

create

  • Creates a Future with a given callback.

    The callback receives a FutureEmitter instance which you can use to resolve/reject values as well as attach cleanup logic when a Computation gets cancelled.

    // Create a Future that resolves 'Hello' after 500ms
    const delayedHello = Future.create((emitter) => {
    
      // Our timeout that resolves the value
      const timeout = setTimeout(() => {
        emitter.success('Hello')
      }, 500);
    
      // Attach cleanup
      emitter.onCancel(() => {
        clearTimeout(timeout);
      });
    });

    Type parameters

    • T

      type of the computed value

    Parameters

    Returns Future<T>

defer

  • A deferred supplier of Future

    Future.defer(() => Future.success('Hello'))
     .get()
     .then(console.log);

    Type parameters

    • T

      type of computed value

    Parameters

    Returns Future<T>

failure

  • failure(error: Error): Future<unknown>

flatten

  • Flattens a Future that resolves into a Future.

    Future.flatten(Future.success(Future.success('Hello World')))
     .get()
     .then(console.log); // Hello World

    Type parameters

    • T

      type of the computed value from the resolved Future.

    Parameters

    Returns Future<T>

fromAction

  • Runs the given action function and resolves with an empty value after it finishes.

    Future.fromAction(() => {
     // do stuff
    })
     .get()
     .then(console.log); // undefined

    Parameters

    Returns Future<unknown>

fromAsync

  • Runs an async function, resolving with the returned Promise's resolved value.

    When the function is called, it receives two values:

    • onCancel: a function that allows you to register an Action that is called when a Computation instance is cancelled.
    • protect: a function that wraps a Promise into a new Promise that prevents it from resolving if the Computation instance is cancelled.
    const delayedHelloWorld = Future.fromAsync(async (onCancel, protect) => {
      // Construct a delayed promise
      const delayed = new Promise((resolve, reject) => {
        // create the timeout
        const timeout = setTimeout(() => {
          resolve('Hello');
        }, 500);
    
        // register cancellation
        onCancel(() => {
          clearTimeout(timeout);
        });
      });
    
      // get the value
      const value = await protect(delayed); // do not resolve if cancelled.
    
      // return the value
      return `${value} World`;
    });
    

    Type parameters

    • T

      type of the value to be returned.

    Parameters

    Returns Future<T>

fromPromise

  • fromPromise<T>(promise: PromiseLike<T>): Future<T>
  • Transforms a Promise instance into a Future.

    Future.fromPromise(Promise.resolve('Hello World'))
     .get()
     .then(console.log); // Hello World

    Type parameters

    • T

      type of the Promise's resolved value

    Parameters

    • promise: PromiseLike<T>

    Returns Future<T>

fromSupplier

  • Runs the given supplier function and resolves with the returned value.

    Future.fromSupplier(() => 'Hello World')
     .get()
     .then(console.log); // Hello World

    Type parameters

    • T

      type of the value to be returned.

    Parameters

    Returns Future<T>

merge

  • merge<S>(sources: S): Future<unknown>
  • Runs the Computation instances from the given array of Future instances at the same time, resolving after all Computation has resolved, ignoring their values.

    const A = Future.timer('Hello', 500);
    const B = Future.timer('World', 400);
    
    Future.merge([A, B])
     .get()
     .then(console.log); // Resolves with an empty value after 500ms

    Type parameters

    • S: Future<any>[]

      type of the Future array

    Parameters

    • sources: S

    Returns Future<unknown>

never

success

  • success<T>(value: T): Future<T>
  • Resolves with the given value

    Future.success('Hello World')
     .get()
     .then(console.log); // Hello World

    Type parameters

    • T

      type of the given value

    Parameters

    • value: T

    Returns Future<T>

timer

  • Resolves a given value after a certain amount of time.

    // a Future that resolves 'Hello' after 500ms
    const delayedHello = Future.timer('Hello', 500);
    
    delayedHello.get()
     .then(console.log);

    Type parameters

    • T

      the type of the computed value

    Parameters

    • value: T

      the value to be resolved into

    • time: number

      the amount of delay in milliseconds

    • Default value scheduler: TimedScheduler = Schedulers.SYNC.TIMED

      the function to schedule the timeout

    Returns Future<T>

zip

  • zip<S, T, R>(sources: S, zipper: function): Future<R>
  • Combines and resolves the outputs of the given array of Future computations.

    const A = Future.timer('Hello', 500);
    const B = Future.timer('World', 250);
    
    Future.zip([A, B], (a, b) => `${a} ${b}`)
     .get()
     .then(console.log); // 'Hello World'
    typeparam

    type of computed values to be received by the zipper function.

    Type parameters

    • S: Future<any>[]

      type of computed values of the Future instances.

    • T: any[]

    • R

      type of the combination value.

    Parameters

    • sources: S

      an array of Future instances.

    • zipper: function

      a function that receives the output of the Future instances and outputs a combination.

        • (...args: T): R
        • Parameters

          • Rest ...args: T

          Returns R

    Returns Future<R>

Schedulers Functions

Const AnimationFrameScheduler

Const AnimationFrameTimedScheduler

Const ImmediateScheduler

Const ImmediateTimedScheduler

Const PromiseScheduler

Const PromisedTimedScheduler

Const SyncScheduler

Const SyncTimedScheduler

Const TimeoutScheduler

Const TimeoutTimedScheduler

Transformers Functions

ambWith

  • Resolves the Computation instance to the fastest given Future.

    Example below resolves the ambWith instance to B as it its faster than A.

    const A = Future.timer('Hello', 500);
    const B = Future.timer('World', 400);
    
    A.compose(Future.ambWith(B))
     .get()
     .then(console.log); // World

    Type parameters

    • T

      Type of the computed value

    Parameters

    Returns FutureTransformer<T, T>

andThen

  • Computes and ignores the composed Future instance and then resolves to the given Future instance after the computation.

    Example below resolves the amb instance to B as it its faster than A.

    const A = Future.timer('Hello', 500);
    const B = Future.timer('World', 400);
    
    A.compose(Future.andThen(B))
     .get()
     .then(console.log); // World after 900s

    Type parameters

    • T

      computed value for the given Future instance

    Parameters

    Returns FutureTransformer<any, T>

andThenPromise

  • Computes and ignores the composed Future instance and then resolves to the given PromiseLike instance after the computation.

    Example below resolves the amb instance to B as it its faster than A.

    const A = Future.timer('Hello', 500);
    const B = Future.timer('World', 400);
    
    A.compose(Future.andThen(B))
     .get()
     .then(console.log); // World after 900s

    Type parameters

    • T

      computed value for the given Future instance

    Parameters

    • other: PromiseLike<T>

    Returns FutureTransformer<any, T>

cache

  • Caches the initial computed value for the Future instance.

    // Create a Future
    const delayedHello = Future.timer('Hello', 500);
    
    // Transform into a cached Future
    const cached = delayedHello.compose(Future.cache());
    
    // Perform the initial computation
    cached.get().then(console.log);
    
    // Try performing another computation
    setTimeout(() => {
     cached.get().then(console.log); // resolves without delay
    }, 550);

    Type parameters

    • T

    Returns FutureTransformer<T, T>

computeOn

  • Schedules the Computation instance on the given scheduler function.

    Future.success('Hello')
     .compose(Future.computeOn(Future.Schedulers.TIMEOUT.NOW))
     .get();
     .then(console.log);

    Type parameters

    • T

      type of the computed value

    Parameters

    • scheduler: Scheduler

      where to perform the computation

    Returns FutureTransformer<T, T>

concat

  • concat<T>(sources: T): Future<any>
  • Concatenates the Computation instance provided by an array of Future instances into a single Computation, and resolves to last concatenated instance.

    const A = Future.timer('Hello', 500);
    const B = Future.timer('World', 400);
    
    Future.concat([A, B])
     .get()
     .then(console.log); // World after 900s

    Type parameters

    • T: Future<any>[]

      computed value for the given Future instance

    Parameters

    • sources: T

    Returns Future<any>

contains

  • Compares the resolved value with the given value and comparer and resolves with the comparer result.

    Default comparer is the Object.is function.

    Type parameters

    • A

      type of the computed value

    • B

      type of the value to be compared with.

    Parameters

    • value: B

      the value to be compared with the resolved value.

    • Default value comparer: Predicate2<A, B> = COMPARER

      a function that compares two values.

    Returns FutureTransformer<A, boolean>

defaultIfEmpty

  • Resolves the Computation instance with the given value if the Computation resolves with a null/undefined value.

    Future.success()
     .compose(Future.defaultIfEmpty('Hello'))
     .get()
     .then(console.log); // Hello

    Type parameters

    • T

      type of computed and given value

    Parameters

    • item: T

      a value to be resolved with

    Returns FutureTransformer<T, T>

delay

  • Delays the Computation instance's value by a certain amount of time.

    Type parameters

    • T

      type of the computed value

    Parameters

    • time: number

      the amount of time to delay

    • Default value scheduler: TimedScheduler = Schedulers.SYNC.TIMED

      where to schedule the time delay

    • Default value immediateRejection: boolean = false

      immediately reject an error

    Returns FutureTransformer<T, T>

doFinally

doOnCancel

doOnEvent

doOnFailure

doOnSuccess

doOnTerminate

filter

flatMap

  • Transforms the resolved value by passing it through a transformer function that returns a Future instance.

    Future.success('Hello World')
     .compose(Future.flatMap(value => Future.success(value)))
     .get()
     .then(console.log); // Hello World

    Type parameters

    • T

      type of the value to be received by the transformer.

    • R

      type of the valeu to be returned by the transformer.

    Parameters

    Returns FutureTransformer<T, R>

ignoreElement

map

  • Transforms the resolved value of the given Future.

    const A = Future.timer('Hello', 500);
    
    const greeting = A.compose(Future.map(value => `${value} World`));
    
    greeting.get().then(console.log); // Hello World

    Type parameters

    • T

      type of the received computed value

    • R

      type of the resulting computed value from the transformer function

    Parameters

    • mapper: Function<T, R>

      a function that transforms the resolved value

    Returns FutureTransformer<T, R>

mergeWith

observeOn

onErrorResume

  • Resolves a rejected Computation by shifting towards the computation of the returned Future from the given function.

    Future.failure(new Error('Bad'))
     .compose(Future.onErrorResume(() => Future.success('Hello World')))
     .get()
     .then(console.log);

    Type parameters

    • T

      type of the computed value and resumed function result

    Parameters

    • resume: Function<Error, Future<T>>

      a function which accepts the rejected Error and returns a Future to shift with.

    Returns FutureTransformer<T, T>

onErrorResumeNext

  • Resolves a rejected Computation by shifting towards the computation of the given Future.

    Future.failure(new Error('Bad'))
     .compose(Future.onErrorResumeNext(Future.success('Hello World')))
     .get()
     .then(console.log);

    Type parameters

    • T

      type of the computed value and resumed function result

    Parameters

    Returns FutureTransformer<T, T>

onErrorReturn

  • Resolves a rejected Computation with the returned value from a given function.

    Future.failure(new Error('Bad'))
     .compose(Future.onErrorReturn(() => 'Hello World'))
     .get()
     .then(console.log);

    Type parameters

    • T

      type of the computed value and resumed function result

    Parameters

    • resume: Function<Error, T>

      a function which accepts the rejected Error

    Returns FutureTransformer<T, T>

onErrorReturnItem

  • Resolves a rejected Computation with the given item

    Future.failure(new Error('Bad'))
     .compose(Future.onErrorReturnItem('Hello World'))
     .get()
     .then(console.log);

    Type parameters

    • T

      type of the computed and given item

    Parameters

    • item: T

      item to resolve with after rejection

    Returns FutureTransformer<T, T>

retry

retryCounted

  • Retries the rejected Computation rejecting with the recent error if it fails to resolve with the given amount of retries.

    proneToErrorFuture.compose(Future.retryCounted(5));

    Type parameters

    • T

      the type of the computed value

    Parameters

    • count: number

      the amount of retries

    Returns FutureTransformer<T, T>

retryTimed

  • Retries a rejected Computation until a certain amount of time or, if provided, the given predicate returns true, rejecting with the recent error.

    proneToErrorFuture.compose(Future.retryTimed(500));

    Type parameters

    • T

      type of the computed value

    Parameters

    • time: number

      The amount of time

    • Default value scheduler: TimedScheduler = Schedulers.SYNC.TIMED

      Where to schedule the timeout

    • Optional until: Predicate<Error>

      a predicate which receives the current rejected error.

    Returns FutureTransformer<T, T>

retryUntil

subscribeOn

  • Schedules the Computation instance's subscription with the given scheduler.

    Similar to computeOn, except that the Computation in subscribeOn begins synchronously.

    Future.success('Hello')
     .compose(Future.subscribeOn(Future.Schedulers.TIMEOUT.NOW))
     .get();
     .then(console.log);

    Type parameters

    • T

      type of the computed value

    Parameters

    • scheduler: Scheduler

      where to perform the computation

    Returns FutureTransformer<T, T>

switchIfEmpty

  • Resolves the Computation instance with the given value if the Computation resolves with a null/undefined value.

    Future.success()
     .compose(Future.defaultIfEmpty('Hello'))
     .get()
     .then(console.log); // Hello

    Type parameters

    • T

      type of computed and given value

    Parameters

    Returns FutureTransformer<T, T>

timeout

  • Rejects with TimeoutError when this Future fails to resolve within a certain amount of time.

    // A Future that resolves after 500ms
    const delayedHello = Future.timer('Hello', 500);
    
    // Compose a new Future which only allows resolution within 400ms
    const strict = delayedHello.compose(Future.timeout(400));
    
    // Begin the computation and expect an error
    strict.get()
     .catch(console.log);

    Type parameters

    • T

    Parameters

    • time: number

      the amount of time

    • Default value scheduler: TimedScheduler = Schedulers.SYNC.TIMED

      where to schedule the timeout

    Returns FutureTransformer<T, T>

zipWith

  • Combines and resolves the output of this Future computation and the other Future computation.

    const A = Future.timer('Hello', 500);
    const B = Future.timer('World', 250);
    
    A.compose(Future.zipWith(B, (a, b) => `${a} ${b}`))
     .get()
     .then(console.log); // 'Hello World'

    Type parameters

    • A

      type of computed value for this Future.

    • B

      type of computed value for the other Future.

    • R

      type of the combination value.

    Parameters

    • other: Future<B>

      a Future instance.

    • zipper: Function2<A, B, R>

      a function that receives the output of the two Future instances and outputs a combination.

    Returns FutureTransformer<A, R>

Generated using TypeDoc