@babybeet/execute-until
TypeScript icon, indicating that this package has built-in type declarations

1.0.1 • Public • Published

execute-until

A function that returns a promise that will only resolve when the predicate function resolves to true for async predicate or returns true for sync version. This is useful for cases when you need to perform some long running operation before proceeding.

The predicate function will be repeatedly called until it resolves to true which causes the asynchronous execution to proceed. By default, the predicate function is called every 500ms.

Please note that this function will reject when the predicate function throws an error/rejects at most 3 times.

Table of contents

Installation

  • npm

    npm i -S @babybeet/execute-until
    
  • pnpm

    pnpm i -S @babybeet/execute-until
    
  • yarn

    yarn add @babybeet/execute-until
    

Example usage

import { executeUntil } from '@babybeet/execute-until';

...

await executeUntil(async () => {
  const batchOfDatabaseRows = await fetchNextBatchOfDatabaseRows();

  if (batchOfDatabaseRows.length === 0) {
    /**
     * This will cause `executeUntil()` to terminate.
     */
    return true;
  }

  doSomethingWithBatch(batchOfDatabaseRows);

  /**
   * This will cause `executeUntil()` to run the predicate again.
   */
  return false;
});

doSomethingElseAfterProcessingAllDatabaseRows();

...

/@babybeet/execute-until/

    Package Sidebar

    Install

    npm i @babybeet/execute-until

    Weekly Downloads

    0

    Version

    1.0.1

    License

    MIT

    Unpacked Size

    15 kB

    Total Files

    14

    Last publish

    Collaborators

    • babybeet