Important: This documentation covers Yarn 1 (Classic).
For Yarn 2+ docs and migration guide, see yarnpkg.com.

Package detail

async-wait-until

devlato422.2kMIT2.0.27TypeScript support: included

Waits until the given predicate function returns a truthy value, then resolves

apply, when, async, then, promise, wait, timeout, interval, until, for, while, predicate, resolve, reject

readme

async-wait-until

A lightweight, zero-dependency library for waiting asynchronously until a specific condition is met. Works in any JavaScript environment that supports Promises, including older Node.js versions and browsers (with polyfills if necessary).

npm version npm downloads MIT License Maintainability

📖 Detailed Documentation

For detailed documentation, visit https://devlato.github.io/async-wait-until/


🚀 Installation

Install using npm:

npm install async-wait-until

The library includes UMD, CommonJS, and ESM bundles, so you can use it in any environment.

import { waitUntil } from 'async-wait-until';

// Example: Wait for an element to appear
await waitUntil(() => document.querySelector('#target') !== null);

🛠️ How to Use

Basic Example: Wait for a DOM Element

import { waitUntil } from 'async-wait-until';

const waitForElement = async () => {
  // Wait for an element with the ID "target" to appear
  const element = await waitUntil(() => document.querySelector('#target'), { timeout: 5000 });
  console.log('Element found:', element);
};

waitForElement();

Handling Timeouts

If the condition is not met within the timeout, a TimeoutError is thrown.

import { waitUntil, TimeoutError } from 'async-wait-until';

const waitForElement = async () => {
  try {
    const element = await waitUntil(() => document.querySelector('#target'), { timeout: 5000 });
    console.log('Element found:', element);
  } catch (error) {
    if (error instanceof TimeoutError) {
      console.error('Timeout: Element not found');
    } else {
      console.error('Unexpected error:', error);
    }
  }
};

waitForElement();

📚 API

waitUntil(predicate, options)

Waits for the predicate function to return a truthy value and resolves with that value.

Parameters:

Name Type Required Default Description
predicate Function ✅ Yes - A function that returns a truthy value (or a Promise for one).
options.timeout number 🚫 No 5000 ms Maximum wait time before throwing TimeoutError. Use WAIT_FOREVER for no timeout.
options.intervalBetweenAttempts number 🚫 No 50 ms Interval between predicate evaluations.

💡 Recipes

Wait Indefinitely

Use WAIT_FOREVER to wait without a timeout:

import { waitUntil, WAIT_FOREVER } from 'async-wait-until';

await waitUntil(() => someCondition, { timeout: WAIT_FOREVER });

Adjust Retry Interval

Change how often the predicate is evaluated:

await waitUntil(() => someCondition, { intervalBetweenAttempts: 1000 }); // Check every 1 second

🧪 Development and Testing

Contributions are welcome! To contribute:

  1. Fork and clone the repository.
  2. Install dependencies: npm install.
  3. Use the following commands:

  4. Run Tests: npm test

  5. Lint Code: npm run lint
  6. Format Code: npm run format
  7. Build Library: npm run build
  8. Generate Docs: npm run docs

changelog

Changelog

2.0.24

  • Bump version to 2.0.24 and add standard files (#68) (github-actions[bot]) [cb2c5aa]
  • Update the project changelog (github-actions[bot]) [8a63b3b]

2.0.23

  • Auto-release to GH (#67) (github-actions[bot]) [67ac746]
  • Update the project changelog (github-actions[bot]) [263ad5d]

2.0.22

  • Remove the tag file download (#66) (github-actions[bot]) [eb0baa0]
  • Update the project changelog (github-actions[bot]) [76843aa]

2.0.21

  • Add missing tagging step id (#65) (github-actions[bot]) [c6cdf39]
  • Update the project changelog (github-actions[bot]) [a368c83]

2.0.20

  • Fix typo in publish pipeline (#64) (github-actions[bot]) [ce7359a]

2.0.19

  • Update package.json (#63) (Denis Tokarev) [e659ebb]
  • Bump undici from 6.21.0 to 6.21.1 in the npm_and_yarn group (#62) (dependabot[bot]) [cb7dcbe]
  • Only trigger the publish workflow when the release is tagged (Denis Tokarev) [95b3e9f]
  • Update the project changelog (github-actions[bot]) [9f60b68]
  • Ignore CHANGELOG.md in release.yml (Denis Tokarev) [845ab5d]
  • Update the project changelog (github-actions[bot]) [e3a7de5]

2.0.18

  • Release 2.0.18 (#61) (github-actions[bot]) [a1b08be]
  • Update the project changelog (github-actions[bot]) [a676bea]

2.0.17

  • Release 2.0.17 (#60) (github-actions[bot]) [3b9442b]
  • Update the project changelog (github-actions[bot]) [2a673a0]
  • Update the project changelog (github-actions[bot]) [34d7511]
  • Update the project changelog (github-actions[bot]) [f33c8eb]
  • Update the project changelog (github-actions[bot]) [65c337e]
  • Update the project changelog (github-actions[bot]) [824b16d]
  • Update the project changelog (github-actions[bot]) [296e634]
  • Update the project changelog (github-actions[bot]) [50d3206]
  • Update the project changelog (github-actions[bot]) [ed554ce]
  • Update the project changelog (github-actions[bot]) [4b079d8]
  • Update the project changelog (github-actions[bot]) [c230436]
  • Update the project changelog (github-actions[bot]) [3482dd8]
  • Update the project changelog (github-actions[bot]) [d1db247]
  • Update the project changelog (github-actions[bot]) [be4ace2]
  • Update the project changelog (github-actions[bot]) [d9deb10]
  • Update the project changelog (github-actions[bot]) [507319a]
  • Update the project changelog (github-actions[bot]) [8b5e494]
  • Update the project changelog (github-actions[bot]) [06d0490]
  • Update the project changelog (github-actions[bot]) [88f3db7]
  • Update the project changelog (github-actions[bot]) [9294bb6]
  • Update the project changelog (github-actions[bot]) [e46f9fb]
  • Update the project changelog (github-actions[bot]) [68e6086]
  • Update the project changelog (github-actions[bot]) [edbfcd4]
  • Update the project changelog (github-actions[bot]) [a913b1a]
  • Update the project changelog (github-actions[bot]) [cccb781]
  • Update the project changelog (github-actions[bot]) [59b0e6b]
  • Update the project changelog (github-actions[bot]) [9f09b49]
  • Update the project changelog (github-actions[bot]) [4735728]
  • Update the project changelog (github-actions[bot]) [dff1c2e]
  • Update the project changelog (github-actions[bot]) [3bab86b]
  • Update the project changelog (github-actions[bot]) [c5efb94]
  • Update the project changelog (github-actions[bot]) [dee49ef]
  • Update the project changelog (github-actions[bot]) [ef20b13]
  • Update the project changelog (github-actions[bot]) [0fd1d09]
  • Update the project changelog (github-actions[bot]) [0017049]
  • Update the project changelog (github-actions[bot]) [61936c8]
  • Update the project changelog (github-actions[bot]) [0642ec2]
  • Update the project changelog (github-actions[bot]) [b241ad1]
  • Update the project changelog (github-actions[bot]) [1c3cb87]
  • Update the project changelog (github-actions[bot]) [6e0cce2]
  • Update the project changelog (github-actions[bot]) [09009d9]
  • Update the project changelog (github-actions[bot]) [ff8763c]
  • Update the project changelog (github-actions[bot]) [99c26a7]
  • Update the project changelog (github-actions[bot]) [057ed1e]
  • Update the project changelog (github-actions[bot]) [02b4cf7]
  • Update the project changelog (github-actions[bot]) [7dd237b]
  • Update the project changelog (github-actions[bot]) [702abea]
  • Update the project changelog (github-actions[bot]) [f563d80]
  • Update the project changelog (github-actions[bot]) [dc8cc80]
  • Update the project changelog (github-actions[bot]) [620eff0]
  • Update the project changelog (github-actions[bot]) [93ec81a]
  • Update the project changelog (github-actions[bot]) [9629e11]
  • Update the project changelog (github-actions[bot]) [196dec5]
  • Update the project changelog (github-actions[bot]) [4cb0989]
  • Update the project changelog (github-actions[bot]) [4357168]
  • Update the project changelog (github-actions[bot]) [c392d02]
  • Update the project changelog (github-actions[bot]) [5323d22]
  • Update the project changelog (github-actions[bot]) [4b0f1e9]
  • Update the project changelog (github-actions[bot]) [dfa9d1d]
  • Update the project changelog (github-actions[bot]) [409a048]
  • Update the project changelog (github-actions[bot]) [812bd6f]
  • Update the project changelog (github-actions[bot]) [fca0bc3]
  • Update the project changelog (github-actions[bot]) [b21c7e6]
  • Update the project changelog (github-actions[bot]) [1b084b0]
  • Update the project changelog (github-actions[bot]) [75f1e00]
  • Update the project changelog (github-actions[bot]) [6b88368]
  • Update the project changelog (github-actions[bot]) [c2a92ca]
  • Update the project changelog (github-actions[bot]) [ee273ab]
  • Update the project changelog (github-actions[bot]) [927f194]
  • Update the project changelog (github-actions[bot]) [88228e0]
  • Update the project changelog (github-actions[bot]) [4b07f62]
  • Update the project changelog (github-actions[bot]) [9529a8c]
  • Update the project changelog (github-actions[bot]) [ebcb9ae]
  • Update the project changelog (github-actions[bot]) [f577b64]
  • Update the project changelog (github-actions[bot]) [42305ca]
  • Update the project changelog (github-actions[bot]) [9b0f9f9]
  • Update the project changelog (github-actions[bot]) [9fdb766]
  • Update the project changelog (github-actions[bot]) [c30e96d]
  • Update the project changelog (github-actions[bot]) [6db41b4]
  • Update the project changelog (github-actions[bot]) [3236888]
  • Update the project changelog (github-actions[bot]) [df4d82d]
  • Update the project changelog (github-actions[bot]) [60b67e5]
  • Update the project changelog (github-actions[bot]) [7ea82b5]
  • Update the project changelog (github-actions[bot]) [d993b66]
  • Update the project changelog (github-actions[bot]) [b12ca88]
  • Update the project changelog (github-actions[bot]) [cd7ab4e]
  • Update the project changelog (github-actions[bot]) [03a440e]
  • Update the project changelog (github-actions[bot]) [bc73fdc]
  • Update the project changelog (github-actions[bot]) [3f55211]
  • Update the project changelog (github-actions[bot]) [bd7aa90]
  • Update the project changelog (github-actions[bot]) [3763921]
  • Update the project changelog (github-actions[bot]) [5ae4c34]
  • Update the project changelog (github-actions[bot]) [2e0d4a1]
  • Update the project changelog (github-actions[bot]) [89da19a]
  • Update the project changelog (github-actions[bot]) [961e9bd]
  • Update the project changelog (github-actions[bot]) [5974ad6]
  • Update the project changelog (github-actions[bot]) [23a8adc]
  • Update the project changelog (github-actions[bot]) [ee44c2e]
  • Update the project changelog (github-actions[bot]) [b6fcd40]
  • Update the project changelog (github-actions[bot]) [4781e92]
  • Update the project changelog (github-actions[bot]) [ea8c362]
  • Update the project changelog (github-actions[bot]) [1a735f4]
  • Update the project changelog (github-actions[bot]) [f40b1fc]
  • Update the project changelog (github-actions[bot]) [cb49108]
  • Update the project changelog (github-actions[bot]) [35e4661]
  • Update the project changelog (github-actions[bot]) [5b29653]
  • Update the project changelog (github-actions[bot]) [eedab45]
  • Update the project changelog (github-actions[bot]) [4ff6086]
  • Update the project changelog (github-actions[bot]) [b44ee38]
  • Update the project changelog (github-actions[bot]) [321cc7a]
  • Update the project changelog (github-actions[bot]) [80e1e15]
  • Update the project changelog (github-actions[bot]) [f88501d]
  • Update the project changelog (github-actions[bot]) [e53aba8]
  • Update the project changelog (github-actions[bot]) [07868e6]
  • Update the project changelog (github-actions[bot]) [cc02cc1]
  • Update the project changelog (github-actions[bot]) [e0db6d0]
  • Update the project changelog (github-actions[bot]) [fad3fc4]
  • Update the project changelog (github-actions[bot]) [5cc6a9a]
  • Update the project changelog (github-actions[bot]) [5f5c123]
  • Update the project changelog (github-actions[bot]) [919d4cc]
  • Update the project changelog (github-actions[bot]) [7846820]
  • Update the project changelog (github-actions[bot]) [02cf6e9]
  • Update the project changelog (github-actions[bot]) [0455fd7]
  • Update the project changelog (github-actions[bot]) [5252e50]
  • Update the project changelog (github-actions[bot]) [abc8bb0]
  • Update the project changelog (github-actions[bot]) [694345f]
  • Update the project changelog (github-actions[bot]) [ebc01d0]
  • Update the project changelog (github-actions[bot]) [43f2859]
  • Update the project changelog (github-actions[bot]) [edfe01a]
  • Update the project changelog (github-actions[bot]) [d7178ef]
  • Update the project changelog (github-actions[bot]) [8885113]
  • Update the project changelog (github-actions[bot]) [24a58a5]
  • Update the project changelog (github-actions[bot]) [727674b]
  • Update the project changelog (github-actions[bot]) [99682d8]
  • Update the project changelog (github-actions[bot]) [f522d57]
  • Update the project changelog (github-actions[bot]) [02c6ab5]
  • Update the project changelog (github-actions[bot]) [37548de]
  • Update the project changelog (github-actions[bot]) [9d6761d]
  • Update the project changelog (github-actions[bot]) [930d776]
  • Update the project changelog (github-actions[bot]) [edafa0b]
  • Update the project changelog (github-actions[bot]) [53fe293]
  • Update the project changelog (github-actions[bot]) [bd875cb]
  • Update the project changelog (github-actions[bot]) [e42da0f]
  • Update the project changelog (github-actions[bot]) [ca8e74e]
  • Update the project changelog (github-actions[bot]) [4890eaf]
  • Update the project changelog (github-actions[bot]) [8a297a6]
  • Update the project changelog (github-actions[bot]) [7107f77]
  • Update the project changelog (github-actions[bot]) [d686808]
  • Update the project changelog (github-actions[bot]) [2881378]
  • Update the project changelog (github-actions[bot]) [7dbb5b0]
  • Update the project changelog (github-actions[bot]) [847a723]
  • Update the project changelog (github-actions[bot]) [0b321a5]
  • Update the project changelog (github-actions[bot]) [6b5412b]
  • Update the project changelog (github-actions[bot]) [f6c663d]
  • Update the project changelog (github-actions[bot]) [186fb33]
  • Update the project changelog (github-actions[bot]) [304ad4d]
  • Update the project changelog (github-actions[bot]) [fda117e]
  • Update the project changelog (github-actions[bot]) [8351db1]
  • Update the project changelog (github-actions[bot]) [2476f34]
  • Update the project changelog (github-actions[bot]) [4b55adc]

2.0.16

  • Release 2.0.16 (#59) (github-actions[bot]) [7c9a91d]
  • Update the project changelog (github-actions[bot]) [7b4721a]

2.0.15

  • Use deploykeys (#58) (github-actions[bot]) [d00c644]

2.0.14

  • 2.0.14 (#57) (github-actions[bot]) [75a1ac6]

2.0.13

  • Prepare 2.0.13-RC (#55) (Denis Tokarev) [e5e3094]
  • Update README.md (#54) (Denis Tokarev) [4255dfa]

2.0.12

  • Fix #29 (#30) (github-actions[bot]) [83482cc]
  • Fix tests (#28) (github-actions[bot]) [9d6f73d]

2.0.10

  • Update the project changelog (github-actions[bot]) [ac4486b]
  • Bump version to 2.0.10 (Denis Tokarev) [af4e364]
  • Update CI to publish only when integration tests has passed (#26) (github-actions[bot]) [8b06c25]
  • Create codeql-analysis.yml (#25) (Denis Tokarev) [668b957]

2.0.9

  • Auto-release and auto-publish daily when possible (#24) (github-actions[bot]) [4d3d2cc]
  • Add re-labeling (Denis Tokarev) [8641a51]
  • Update auto-labeling (Denis Tokarev) [d47ce0c]
  • Set test timeout to 10s (Denis Tokarev) [154f35e]
  • Update the CI configuration to properly set PR bodies (Denis Tokarev) [71f8658]
  • Update codeclimate config (Denis Tokarev) [208bd20]
  • Update codeclimate config (Denis Tokarev) [87d3f35]
  • Fix (Denis Tokarev) [d151065]
  • Update scripts (Denis Tokarev) [7763e70]
  • Upgrade all deps and prepare v2.0.9 (Denis Tokarev) [a999686]

2.0.8

  • Adjust spelling in readme and index (#21) (Lukasz Kokot) [7e7178d]
  • GH_PAT -> GITHUB_TOKEN (Denis Tokarev) [ef0219c]
  • Fix workflows (Denis Tokarev) [5b1fa8f]
  • Upgrade automerge action (Denis Tokarev) [7c72d54]
  • Fix timeout default value in README (#20) (Simon Smith) [2a51437]

2.0.7

  • Aaa (Denis Tokarev) [dbf7cdf]
  • Update README (#18) (github-actions[bot]) [43d46df]

2.0.6

  • Fix #16: add 'module' to package.json, manually fix dependabot alerts (#17) (github-actions[bot]) [2ef43fa]

2.0.5

  • Increase allowed complexity (Denis Tokarev) [f6397ea]
  • Fix #14 (Denis Tokarev) [bfea826]

2.0.4

  • Disable secret scan (#13) (github-actions[bot]) [0ae085e]
  • Update README -> 2.0.4 (#12) (github-actions[bot]) [16cbe84]

2.0.3

  • Further get rid of global variables (#11) (Denis Tokarev) [38ebf94]
  • Further get rid of global variables (Denis Tokarev) [a1cc16f]

2.0.2

  • Fix issue w/wrong setTimeout call (#10) (github-actions[bot]) [8aa1f79]

2.0.1

  • Fix accidental index.d.ts file removal (#8) (github-actions[bot]) [742d063]
  • Delete docs directory (Denis Tokarev) [f33d9b1]
  • Delete index.md (Denis Tokarev) [c1219a4]
  • Set theme jekyll-theme-slate (Denis Tokarev) [b03c05c]

2.0.0

  • Early 2.0 (#5) (github-actions[bot]) [960d93a]

1.2.6

  • Set 1.2.6 (Denis Tokarev) [06d2ce2]
  • Update package.json (Denis Tokarev) [5d801b2]
  • Update .travis.yml (Denis Tokarev) [a7dc1e7]
  • Update README.md (Denis Tokarev) [05c3a74]
  • Update .travis.yml (Denis Tokarev) [f860c6e]

1.2.5

  • Update index.d.ts (Denis Tokarev) [2250371]
  • Update README.md (Denis Tokarev) [34f0703]
  • Update README.md (Denis Tokarev) [37eda17]
  • Update README.md (Denis Tokarev) [1cf89a3]
  • Update README.md (Denis Tokarev) [2ec477c]

1.2.4

  • Update Readme (Denis Tokarev) [f062270]

1.2.3

  • Update token (Denis Tokarev) [c4bf260]

1.2.2

  • Add Node 10 (Denis Tokarev) [001a0d6]

1.2.1

  • Fix codeclimate config (Denis Tokarev) [db8db68]

1.2.0

  • Add TypeScript definitions (Denis Tokarev) [8ea2d41]

1.1.7

  • Update token (Denis Tokarev) [83f880c]

1.1.6

  • Release 1.1.6 (Denis Tokarev) [652a576]
  • Remove Bluebird (Denis Tokarev) [18de9d6]

1.1.5

  • [*] Update README (devlato) [e5b0cc3]

1.1.4

  • [+] Add Issues Badge (devlato) [7190d49]
  • [*] Fix codeclimate config (devlato) [28763d6]
  • [*] Fix codeclimate config (devlato) [44cac7e]

1.1.3

  • [*] Download rank (devlato) [8ea78c5]

1.1.2

  • [*] Rating support (devlato) [23a2b13]
  • [*] Fix (devlato) [15c740e]
  • [*] Fix code climate (devlato) [b63b926]
  • [*] Better CI & CD Integration (devlato) [bf113e2]
  • [*] Ignore travis file for building (devlato) [219752f]
  • [*] Travis CI Integration (devlato) [d9df56e]
  • [*] Update README, exclude files from shipping (devlato) [8a2b2bb]

1.1.0

  • [*] Add support for predicate rvalue (devlato) [b63e0e9]

1.0.3

  • [*] Integrate Husky (devlato) [1dd0165]

1.0.2

  • [*] Update README (devlato) [c0ab7df]

1.0.1

  • [*] Fix package name (devlato) [95c3bab]

1.0.0

  • [+] Implement waitUntil 1.0, provide 100% coverage with tests (devlato) [d6b7e7a]