Options
All
  • Public
  • Public/Protected
  • All
Menu

Module "index"

module

tilly

Index

Type aliases

ExecutorFunction

ExecutorFunction<T>: (resolve: ResolveFunction<T>, reject: RejectFunction) => void

Type parameters

  • T

Type declaration

PromiseCreationArgument

PromiseCreationArgument<T>: Promise<T> | ExecutorFunction<T> | T

Type parameters

  • T

RejectFunction

RejectFunction: (reason?: any) => void

Type declaration

    • (reason?: any): void
    • Parameters

      • Optional reason: any

      Returns void

ResolveFunction

ResolveFunction<T>: (value?: T) => void

Type parameters

  • T

Type declaration

    • (value?: T): void
    • Parameters

      • Optional value: T

      Returns void

RetryExecutorFunction

RetryExecutorFunction<T>: (resolve: ResolveFunction<T>, reject: RejectFunction, index: number, maxRetry: number) => void

Type parameters

  • T

Type declaration

ThenFunction

ThenFunction<T, R>: (value?: T) => R

Type parameters

  • T

  • R

Type declaration

    • (value?: T): R
    • Parameters

      • Optional value: T

      Returns R

Functions

all

  • Shortcut for Promise.all, with the ability to pass the promises as single arguments.

    import { all, ok } from "tilly";
    
    const result = await all(
      ok("Result #1"),
      ok("Result #2"),
      ok("Result #3")
    );
    
    console.log(result);
    // ["Result #1", "Result #2", "Result #3"]

    Type parameters

    • T

    Parameters

    • Rest ...args: PromiseCreationArgument<T>[]

      The first executor function or Promise to resolve.
      The list of executors or Promises to be solved can be passed as individual arguments or as an Array in the first argument.

    Returns Promise<T[]>

cache

  • cache<T>(generator: () => Promise<T>, cacheTime?: number): () => Promise<T>
  • Generate a function that invoke a generator parameter and return the same Promise until this reject or if is elapsed a specified time

    import { cache, ok, ko } from "tilly";
    
    const getResolved = cache(() => ok("Result!"));
    const prom1 = getResolved();
    // wait Promise resolves/rejects…
    const prom2 = getResolved();
    
    console.log(prom1 === prom2);
    // true
    
    const getRejected = cache(() => ko("Error!"));
    const prom3 = getRejected();
    // wait Promise resolves/rejects…
    const prom4 = getRejected();
    
    console.log(prom3 === prom4);
    // false

    Type parameters

    • T

    Parameters

    • generator: () => Promise<T>

      The function that generate the promise to cache

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

    • Default value cacheTime: number = 0

    Returns () => Promise<T>

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

chain

  • Function that allows you to execute a promise followed by a sequence of transformations of the returned value through a chain of calls to the then() method.

    import { chain } from "tilly";
    
    const result = await chain(
      (ok, ko) => {
        ok(2);
      },
      (value) => {
        return value * 2;
      },
      (value) => {
        return value + 3;
      }
    );
    
    console.log(result);
    // 7

    Type parameters

    • T

    • R

    Parameters

    • start: PromiseCreationArgument<T>

      Function/Promise/mixed The Promise, or the executor function for the Promise, or value for the resolved promise

    • Rest ...chain: ThenFunction[]

    Returns Promise<R>

    Result Promise of the chain calls

delay

  • This function delay the resolution of a promise.
    De facto is an alias of sleep() function with inverse not-optional arguments

    import { delay } from "tilly";
    
    const result = delay("wake up!", 3000);
    
    console.log(result);
    // "wake up!"

    Type parameters

    • T

    Parameters

    • data: PromiseCreationArgument<T>

      Promise, or executor function, or data value to pass to sleep() resolution after waiting time expires

    • time: number

      The time to wait before Promise resolution, in milliseconds

    Returns Promise<T>

every

  • Similar to the "all" function but with the "to" function applied to each element.
    This allows to obtain the result of each Promise, both in case of resolution and rejection.

    import { every, ok, ko } from "tilly";
    
    const result = await every(
      ok("Result #1"),
      ko("Error #2"),
      ok("Result #3")
    );
    
    console.log(result);
    // [{"success": true, "payload": "Result #1"},
    //  {"success": false, "error": Error("Error #2")},
    //  {"success": true, "payload": "Result #3"}]
    

    Type parameters

    • T

    Parameters

    • Rest ...proms: PromiseCreationArgument<T>[]

      List of executors or Promises to be solved can be passed as individual arguments or as an Array in the first argument.

    Returns Promise<PromiseToResult<T>[]>

    Result of all Promises

from

  • Shortcut for Promise initialization.

    import { from } from "tilly";
    
    const result = await from((ok, ko) => {
      ok("Resolved!");
    });
    
    console.log(result);
    // "Resolved!"

    Type parameters

    • T

    Parameters

    Returns Promise<T>

ko

  • ko(reason: Error | string): Promise<never>
  • Shortcut for Promise.reject.

    import { ko } from "tilly";
    
    const result = await ko("Something went wrong");
    // Error("Something went wrong")

    Parameters

    • reason: Error | string

    Returns Promise<never>

    Rejected promise

ok

  • ok<T>(value: T): Promise<T>
  • Shortcut for Promise.resolve.

    import { ok } from "tilly";
    
    const result = await ok("Resolved!");
    
    console.log(result);
    // "Resolved!"

    Type parameters

    • T

    Parameters

    • value: T

      The resolved result of the Promise

    Returns Promise<T>

    Resolved promise

race

  • Shortcut for Promise.race, with the ability to pass the promises as single arguments.

    import { race, ok } from "tilly";
    
    const result = await race(
      (resolve) => { setTimeout(() => { resolve("One"); }, 600); },
      (resolve) => { setTimeout(() => { resolve("Two"); }, 200); },
      (resolve) => { setTimeout(() => { resolve("Three"); }, 400); },
    );
    
    console.log(result);
    // "Two"

    Type parameters

    • T

    Parameters

    • Rest ...args: PromiseCreationArgument<T>[]

      The first executor function or Promise to resolve.
      The list of executors or Promises to be solved can be passed as individual arguments or as an Array in the first argument.

    Returns Promise<T>

    The first resolved Promise

retry

  • This function allows you to execute the call to Promise several times in the case of re-rejection, until there is a resolution or the attempts are exhausted.

    import { retry } from "tilly";
    
    const result = await retry(3, (resolve, reject, i, n) => {
      if(i == n) {
        resolve("At the last attempt");
      } else {
        reject(new Error("Failed attempt"));
      }
    });
    
    console.log(result);
    // "At the last attempt"

    Type parameters

    • T

    Parameters

    • maxRetry: number

      The maximum number of attempts to retry to execute the Promise

    • executor: RetryExecutorFunction<T>

      The executor function with the addition of two arguments, the attempt index and the total number of attempts

    Returns Promise<T>

    Result Promise

sleep

  • This function in conjunction with the use of await allows pausing the execution of the current context.
    Without the use of await it allows you to perform a Promise with delay.

    import { sleep } from "tilly";
    
    const result = await sleep(3000, "wake up!");
    
    console.log(result);
    // "wake up!"

    Type parameters

    • T

    Parameters

    • time: number

      The time to wait before Promise resolution, in milliseconds

    • Optional data: PromiseCreationArgument<T>

      Optional. Promise, or executor function, or data value to pass to sleep() resolution after waiting time expires

    Returns Promise<T>

to

  • Function that allows the safe use of await without the need for a try / catch. In case of rejection the error is captured and returned.
    The structure of the result of the function is an object with two properties: "success" true / false and depending on the case "payload" or "error" evaluated.

    import { to, ok, ko } from "tilly";
    
    const result1 = await to(
      ok("Resolved!")
    );
    
    console.log(result1);
    // {"success": true, "payload": "Resolved!"}
    
    const result2 = await to(
      ko("Something went wrong")
    );
    
    console.log(result2);
    // {"success": false, "error": Error("Something went wrong")}

    Type parameters

    • T

    Parameters

    Returns Promise<PromiseToResult<T>>

    Promise with managed result object

Generated using TypeDoc