Options
All
  • Public
  • Public/Protected
  • All
Menu

Module Utilities

Index

Functions

Fill

  • Start everything at the same time, It will return and array of IFillResult

    .Fill() Guide

    We will use this code to simulate an heavy task:

    /**
     * We simulate a heavy task
     * @template T
     * @param {T} i
     * @return {*}  {Promise<T>}
     */
    async function heavyTask<T>(i: T): Promise<T> {
      const r = 1 + Math.round(Math.random() * 9);
      // Sleep for a random duration
      await Utilities.WaitSleep(r * 500);
      if (r % 2 === 0) {
        return i;
      }
      // Throw error if r is odd
      throw new Error('Unexpected result');
    }

    Utilities.Fill will be used to perform many tasks at the same time but you can't sure about its result.

    import { Utilities } from './src/index';
    
    (async () => {
      let result = await Utilities.Fill(
        async () => {
          return heavyTask('First');
        },
        async () => {
          return heavyTask('Second');
        },
        async () => {
          return heavyTask('Third');
        },
      );
    
      console.log(result);
    })();

    Result:

    [ { success: true, result: 'First' },
      { success: false,
        result:
         Error: Unexpected result
             at sleepRandom (/home/chiro/gits/chiro-hiro/noqueue/test1.ts:9:9) },
      { success: true, result: 'Third' } ]

    Results of all given functions will be appeared after all promises are fulfilled.

    export

    Parameters

    • Rest ...asyncFunctions: IQueueFunction[]

      Async function that will be added

    Returns Promise<IFillResult[]>

First

  • It's a race, we're only take the first result. In case, every racer was failed we return last error

    .First() Guide

    Utilities.First could be use for the same task to get the result as soon as possible.

    import { Utilities } from './src/index';
    
    (async () => {
      let result = await Utilities.First(
        async () => {
          return heavyTask('First');
        },
        async () => {
          return heavyTask('Second');
        },
        async () => {
          return heavyTask('Third');
        },
      );
    
      console.log('The fastest is:', result);
    })();

    Result:

    The fastest is: Second

    As you see, we have one winner here. If there are an Error, It's fine too that meant all tasks were failed.

    export

    Parameters

    • Rest ...asyncFunctions: IQueueFunction[]

      Async function that will be added

    Returns Promise<any>

TillSuccess

  • TillSuccess<T>(asyncFunction: IQueueFunction, paddingTime?: number, retries?: number): Promise<T>
  • Try an async function several times till it's successful

    .TillSuccess() Example

    Utilities.TillSuccess could be used to guarantee your request or data reach target.

    import { Utilities } from './src/index';
    
    (async () => {
      let tried = 0;
      // Retries heavy task each 2 second, max retries are 5 times
      let result = await Utilities.TillSuccess(
        async () => {
          tried += 1;
          console.log(`Trying ${tried} time(s)`);
          return heavyTask('Yes');
        },
        2000,
        5,
      );
    
      console.log('Was it successful?', result);
    })();

    Result:

    Trying 1 time(s)
    Trying 2 time(s)
    Was it successful? Yes

    If we're fail all the times, an Error will be threw.

    export

    Type parameters

    • T

      Could be any kind of data

    Parameters

    • asyncFunction: IQueueFunction

      Function need to be retried

    • Default value paddingTime: number = 1000
    • Default value retries: number = 3

    Returns Promise<T>

WaitSleep

  • WaitSleep(duration: number): Promise<undefined>
  • Sleep for a wait

    export

    Parameters

    • duration: number

      Duration in milliseconds

    Returns Promise<undefined>

Generated using TypeDoc