UNPKG

async-wait-until

Version:

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

93 lines (86 loc) โ€ข 20.5 kB
<!DOCTYPE html><html class="default" lang="en" data-base="./"><head><meta charset="utf-8"/><meta http-equiv="x-ua-compatible" content="IE=edge"/><title>Documentation for async-wait-until v2.0.27</title><meta name="description" content="Documentation for Documentation for async-wait-until v2.0.27"/><meta name="viewport" content="width=device-width, initial-scale=1"/><link rel="stylesheet" href="assets/style.css"/><link rel="stylesheet" href="assets/highlight.css"/><script defer src="assets/main.js"></script><script async src="assets/icons.js" id="tsd-icons-script"></script><script async src="assets/search.js" id="tsd-search-script"></script><script async src="assets/navigation.js" id="tsd-nav-script"></script></head><body><script>document.documentElement.dataset.theme = localStorage.getItem("tsd-theme") || "os";document.body.style.display="none";setTimeout(() => window.app?app.showPage():document.body.style.removeProperty("display"),500)</script><header class="tsd-page-toolbar"><div class="tsd-toolbar-contents container"><a href="index.html" class="title">Documentation for async-wait-until v2.0.27</a><div id="tsd-toolbar-links"></div><button id="tsd-search-trigger" class="tsd-widget" aria-label="Search"><svg width="16" height="16" viewBox="0 0 16 16" fill="none" aria-hidden="true"><use href="assets/icons.svg#icon-search"></use></svg></button><dialog id="tsd-search" aria-label="Search"><input role="combobox" id="tsd-search-input" aria-controls="tsd-search-results" aria-autocomplete="list" aria-expanded="true" autocapitalize="off" autocomplete="off" placeholder="Search the docs" maxLength="100"/><ul role="listbox" id="tsd-search-results"></ul><div id="tsd-search-status" aria-live="polite" aria-atomic="true"><div>Preparing search index...</div></div></dialog><a href="#" class="tsd-widget menu" id="tsd-toolbar-menu-trigger" data-toggle="menu" aria-label="Menu"><svg width="16" height="16" viewBox="0 0 16 16" fill="none" aria-hidden="true"><use href="assets/icons.svg#icon-menu"></use></svg></a></div></header><div class="container container-main"><div class="col-content"><div class="tsd-page-title"><h1>Documentation for async-wait-until v2.0.27</h1></div><div class="tsd-panel tsd-typography"><h1 id="async-wait-until" class="tsd-anchor-link">async-wait-until<a href="#async-wait-until" aria-label="Permalink" class="tsd-anchor-icon"><svg viewBox="0 0 24 24" aria-hidden="true"><use href="assets/icons.svg#icon-anchor"></use></svg></a></h1><p>A lightweight, zero-dependency library for waiting asynchronously until a specific condition is met. Works in any JavaScript environment that supports <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise">Promises</a>, including older Node.js versions and browsers (with polyfills if necessary).</p> <p><img src="https://img.shields.io/npm/v/async-wait-until" alt="npm version"> <a href="https://npmjs.org/package/async-wait-until"><img src="https://img.shields.io/npm/dw/async-wait-until" alt="npm downloads"></a> <img src="https://img.shields.io/npm/l/async-wait-until" alt="MIT License"> <a href="https://codeclimate.com/github/devlato/async-wait-until/maintainability"><img src="https://api.codeclimate.com/v1/badges/2a967399786c0d306247/maintainability" alt="Maintainability"></a></p> <h2 id="๐Ÿ“–-detailed-documentation" class="tsd-anchor-link">๐Ÿ“– Detailed Documentation<a href="#๐Ÿ“–-detailed-documentation" aria-label="Permalink" class="tsd-anchor-icon"><svg viewBox="0 0 24 24" aria-hidden="true"><use href="assets/icons.svg#icon-anchor"></use></svg></a></h2><p>For detailed documentation, visit <a href="https://devlato.github.io/async-wait-until/">https://devlato.github.io/async-wait-until/</a></p> <hr> <h2 id="๐Ÿš€-installation" class="tsd-anchor-link">๐Ÿš€ Installation<a href="#๐Ÿš€-installation" aria-label="Permalink" class="tsd-anchor-icon"><svg viewBox="0 0 24 24" aria-hidden="true"><use href="assets/icons.svg#icon-anchor"></use></svg></a></h2><p>Install using npm:</p> <pre><code class="sh"><span class="hl-0">npm</span><span class="hl-1"> </span><span class="hl-2">install</span><span class="hl-1"> </span><span class="hl-2">async-wait-until</span> </code><button type="button">Copy</button></pre> <p>The library includes UMD, CommonJS, and ESM bundles, so you can use it in any environment.</p> <pre><code class="javascript"><span class="hl-3">import</span><span class="hl-1"> { </span><span class="hl-4">waitUntil</span><span class="hl-1"> } </span><span class="hl-3">from</span><span class="hl-1"> </span><span class="hl-2">&#39;async-wait-until&#39;</span><span class="hl-1">;</span><br/><br/><span class="hl-5">// Example: Wait for an element to appear</span><br/><span class="hl-3">await</span><span class="hl-1"> </span><span class="hl-0">waitUntil</span><span class="hl-1">(() </span><span class="hl-6">=&gt;</span><span class="hl-1"> </span><span class="hl-4">document</span><span class="hl-1">.</span><span class="hl-0">querySelector</span><span class="hl-1">(</span><span class="hl-2">&#39;#target&#39;</span><span class="hl-1">) !== </span><span class="hl-6">null</span><span class="hl-1">);</span> </code><button type="button">Copy</button></pre> <hr> <h2 id="๐Ÿ› ๏ธ-how-to-use" class="tsd-anchor-link">๐Ÿ› ๏ธ How to Use<a href="#๐Ÿ› ๏ธ-how-to-use" aria-label="Permalink" class="tsd-anchor-icon"><svg viewBox="0 0 24 24" aria-hidden="true"><use href="assets/icons.svg#icon-anchor"></use></svg></a></h2><h3 id="basic-example-wait-for-a-dom-element" class="tsd-anchor-link">Basic Example: Wait for a DOM Element<a href="#basic-example-wait-for-a-dom-element" aria-label="Permalink" class="tsd-anchor-icon"><svg viewBox="0 0 24 24" aria-hidden="true"><use href="assets/icons.svg#icon-anchor"></use></svg></a></h3><pre><code class="javascript"><span class="hl-3">import</span><span class="hl-1"> { </span><span class="hl-4">waitUntil</span><span class="hl-1"> } </span><span class="hl-3">from</span><span class="hl-1"> </span><span class="hl-2">&#39;async-wait-until&#39;</span><span class="hl-1">;</span><br/><br/><span class="hl-6">const</span><span class="hl-1"> </span><span class="hl-0">waitForElement</span><span class="hl-1"> = </span><span class="hl-6">async</span><span class="hl-1"> () </span><span class="hl-6">=&gt;</span><span class="hl-1"> {</span><br/><span class="hl-1"> </span><span class="hl-5">// Wait for an element with the ID &quot;target&quot; to appear</span><br/><span class="hl-1"> </span><span class="hl-6">const</span><span class="hl-1"> </span><span class="hl-7">element</span><span class="hl-1"> = </span><span class="hl-3">await</span><span class="hl-1"> </span><span class="hl-0">waitUntil</span><span class="hl-1">(() </span><span class="hl-6">=&gt;</span><span class="hl-1"> </span><span class="hl-4">document</span><span class="hl-1">.</span><span class="hl-0">querySelector</span><span class="hl-1">(</span><span class="hl-2">&#39;#target&#39;</span><span class="hl-1">), { </span><span class="hl-4">timeout:</span><span class="hl-1"> </span><span class="hl-8">5000</span><span class="hl-1"> });</span><br/><span class="hl-1"> </span><span class="hl-4">console</span><span class="hl-1">.</span><span class="hl-0">log</span><span class="hl-1">(</span><span class="hl-2">&#39;Element found:&#39;</span><span class="hl-1">, </span><span class="hl-4">element</span><span class="hl-1">);</span><br/><span class="hl-1">};</span><br/><br/><span class="hl-0">waitForElement</span><span class="hl-1">();</span> </code><button type="button">Copy</button></pre> <h3 id="handling-timeouts" class="tsd-anchor-link">Handling Timeouts<a href="#handling-timeouts" aria-label="Permalink" class="tsd-anchor-icon"><svg viewBox="0 0 24 24" aria-hidden="true"><use href="assets/icons.svg#icon-anchor"></use></svg></a></h3><p>If the condition is not met within the timeout, a <code>TimeoutError</code> is thrown.</p> <pre><code class="javascript"><span class="hl-3">import</span><span class="hl-1"> { </span><span class="hl-4">waitUntil</span><span class="hl-1">, </span><span class="hl-4">TimeoutError</span><span class="hl-1"> } </span><span class="hl-3">from</span><span class="hl-1"> </span><span class="hl-2">&#39;async-wait-until&#39;</span><span class="hl-1">;</span><br/><br/><span class="hl-6">const</span><span class="hl-1"> </span><span class="hl-0">waitForElement</span><span class="hl-1"> = </span><span class="hl-6">async</span><span class="hl-1"> () </span><span class="hl-6">=&gt;</span><span class="hl-1"> {</span><br/><span class="hl-1"> </span><span class="hl-3">try</span><span class="hl-1"> {</span><br/><span class="hl-1"> </span><span class="hl-6">const</span><span class="hl-1"> </span><span class="hl-7">element</span><span class="hl-1"> = </span><span class="hl-3">await</span><span class="hl-1"> </span><span class="hl-0">waitUntil</span><span class="hl-1">(() </span><span class="hl-6">=&gt;</span><span class="hl-1"> </span><span class="hl-4">document</span><span class="hl-1">.</span><span class="hl-0">querySelector</span><span class="hl-1">(</span><span class="hl-2">&#39;#target&#39;</span><span class="hl-1">), { </span><span class="hl-4">timeout:</span><span class="hl-1"> </span><span class="hl-8">5000</span><span class="hl-1"> });</span><br/><span class="hl-1"> </span><span class="hl-4">console</span><span class="hl-1">.</span><span class="hl-0">log</span><span class="hl-1">(</span><span class="hl-2">&#39;Element found:&#39;</span><span class="hl-1">, </span><span class="hl-4">element</span><span class="hl-1">);</span><br/><span class="hl-1"> } </span><span class="hl-3">catch</span><span class="hl-1"> (</span><span class="hl-4">error</span><span class="hl-1">) {</span><br/><span class="hl-1"> </span><span class="hl-3">if</span><span class="hl-1"> (</span><span class="hl-4">error</span><span class="hl-1"> </span><span class="hl-6">instanceof</span><span class="hl-1"> </span><span class="hl-9">TimeoutError</span><span class="hl-1">) {</span><br/><span class="hl-1"> </span><span class="hl-4">console</span><span class="hl-1">.</span><span class="hl-0">error</span><span class="hl-1">(</span><span class="hl-2">&#39;Timeout: Element not found&#39;</span><span class="hl-1">);</span><br/><span class="hl-1"> } </span><span class="hl-3">else</span><span class="hl-1"> {</span><br/><span class="hl-1"> </span><span class="hl-4">console</span><span class="hl-1">.</span><span class="hl-0">error</span><span class="hl-1">(</span><span class="hl-2">&#39;Unexpected error:&#39;</span><span class="hl-1">, </span><span class="hl-4">error</span><span class="hl-1">);</span><br/><span class="hl-1"> }</span><br/><span class="hl-1"> }</span><br/><span class="hl-1">};</span><br/><br/><span class="hl-0">waitForElement</span><span class="hl-1">();</span> </code><button type="button">Copy</button></pre> <hr> <h2 id="๐Ÿ“š-api" class="tsd-anchor-link">๐Ÿ“š API<a href="#๐Ÿ“š-api" aria-label="Permalink" class="tsd-anchor-icon"><svg viewBox="0 0 24 24" aria-hidden="true"><use href="assets/icons.svg#icon-anchor"></use></svg></a></h2><h3 id="waituntilpredicate-options" class="tsd-anchor-link"><code>waitUntil(predicate, options)</code><a href="#waituntilpredicate-options" aria-label="Permalink" class="tsd-anchor-icon"><svg viewBox="0 0 24 24" aria-hidden="true"><use href="assets/icons.svg#icon-anchor"></use></svg></a></h3><p>Waits for the <code>predicate</code> function to return a truthy value and resolves with that value.</p> <p><strong>Parameters:</strong></p> <table> <thead> <tr> <th>Name</th> <th>Type</th> <th>Required</th> <th>Default</th> <th>Description</th> </tr> </thead> <tbody> <tr> <td><code>predicate</code></td> <td><code>Function</code></td> <td>โœ… Yes</td> <td>-</td> <td>A function that returns a truthy value (or a Promise for one).</td> </tr> <tr> <td><code>options.timeout</code></td> <td><code>number</code></td> <td>๐Ÿšซ No</td> <td><code>5000</code> ms</td> <td>Maximum wait time before throwing <code>TimeoutError</code>. Use <code>WAIT_FOREVER</code> for no timeout.</td> </tr> <tr> <td><code>options.intervalBetweenAttempts</code></td> <td><code>number</code></td> <td>๐Ÿšซ No</td> <td><code>50</code> ms</td> <td>Interval between predicate evaluations.</td> </tr> </tbody> </table> <hr> <h2 id="๐Ÿ’ก-recipes" class="tsd-anchor-link">๐Ÿ’ก Recipes<a href="#๐Ÿ’ก-recipes" aria-label="Permalink" class="tsd-anchor-icon"><svg viewBox="0 0 24 24" aria-hidden="true"><use href="assets/icons.svg#icon-anchor"></use></svg></a></h2><h3 id="wait-indefinitely" class="tsd-anchor-link">Wait Indefinitely<a href="#wait-indefinitely" aria-label="Permalink" class="tsd-anchor-icon"><svg viewBox="0 0 24 24" aria-hidden="true"><use href="assets/icons.svg#icon-anchor"></use></svg></a></h3><p>Use <code>WAIT_FOREVER</code> to wait without a timeout:</p> <pre><code class="javascript"><span class="hl-3">import</span><span class="hl-1"> { </span><span class="hl-4">waitUntil</span><span class="hl-1">, </span><span class="hl-4">WAIT_FOREVER</span><span class="hl-1"> } </span><span class="hl-3">from</span><span class="hl-1"> </span><span class="hl-2">&#39;async-wait-until&#39;</span><span class="hl-1">;</span><br/><br/><span class="hl-3">await</span><span class="hl-1"> </span><span class="hl-0">waitUntil</span><span class="hl-1">(() </span><span class="hl-6">=&gt;</span><span class="hl-1"> </span><span class="hl-4">someCondition</span><span class="hl-1">, { </span><span class="hl-4">timeout:</span><span class="hl-1"> </span><span class="hl-7">WAIT_FOREVER</span><span class="hl-1"> });</span> </code><button type="button">Copy</button></pre> <h3 id="adjust-retry-interval" class="tsd-anchor-link">Adjust Retry Interval<a href="#adjust-retry-interval" aria-label="Permalink" class="tsd-anchor-icon"><svg viewBox="0 0 24 24" aria-hidden="true"><use href="assets/icons.svg#icon-anchor"></use></svg></a></h3><p>Change how often the predicate is evaluated:</p> <pre><code class="javascript"><span class="hl-3">await</span><span class="hl-1"> </span><span class="hl-0">waitUntil</span><span class="hl-1">(() </span><span class="hl-6">=&gt;</span><span class="hl-1"> </span><span class="hl-4">someCondition</span><span class="hl-1">, { </span><span class="hl-4">intervalBetweenAttempts:</span><span class="hl-1"> </span><span class="hl-8">1000</span><span class="hl-1"> }); </span><span class="hl-5">// Check every 1 second</span> </code><button type="button">Copy</button></pre> <hr> <h2 id="๐Ÿงช-development-and-testing" class="tsd-anchor-link">๐Ÿงช Development and Testing<a href="#๐Ÿงช-development-and-testing" aria-label="Permalink" class="tsd-anchor-icon"><svg viewBox="0 0 24 24" aria-hidden="true"><use href="assets/icons.svg#icon-anchor"></use></svg></a></h2><p>Contributions are welcome! To contribute:</p> <ol> <li>Fork and clone the repository.</li> <li>Install dependencies: <code>npm install</code>.</li> <li>Use the following commands:</li> </ol> <ul> <li><strong>Run Tests:</strong> <code>npm test</code></li> <li><strong>Lint Code:</strong> <code>npm run lint</code></li> <li><strong>Format Code:</strong> <code>npm run format</code></li> <li><strong>Build Library:</strong> <code>npm run build</code></li> <li><strong>Generate Docs:</strong> <code>npm run docs</code></li> </ul> <hr> <h2 id="๐Ÿ“-links" class="tsd-anchor-link">๐Ÿ“ Links<a href="#๐Ÿ“-links" aria-label="Permalink" class="tsd-anchor-icon"><svg viewBox="0 0 24 24" aria-hidden="true"><use href="assets/icons.svg#icon-anchor"></use></svg></a></h2><ul> <li><a href="media/LICENSE">License</a></li> <li><a href="https://devlato.github.io/async-wait-until/">Detailed Documentation</a></li> <li><a href="media/CHANGELOG.md">Changelog</a> - Track version updates and changes</li> <li><a href="media/CONTRIBUTING.md">Contributing Guidelines</a> - How to contribute to the project</li> <li><a href="media/CODE_OF_CONDUCT.md">Code of Conduct</a> - Community standards and expectations</li> </ul> </div></div><div class="col-sidebar"><div class="page-menu"><div class="tsd-navigation settings"><details class="tsd-accordion"><summary class="tsd-accordion-summary"><svg width="20" height="20" viewBox="0 0 24 24" fill="none" aria-hidden="true"><use href="assets/icons.svg#icon-chevronDown"></use></svg><h3>Settings</h3></summary><div class="tsd-accordion-details"><div class="tsd-filter-visibility"><span class="settings-label">Member Visibility</span><ul id="tsd-filter-options"><li class="tsd-filter-item"><label class="tsd-filter-input"><input type="checkbox" id="tsd-filter-protected" name="protected"/><svg width="32" height="32" viewBox="0 0 32 32" aria-hidden="true"><rect class="tsd-checkbox-background" width="30" height="30" x="1" y="1" rx="6" fill="none"></rect><path class="tsd-checkbox-checkmark" d="M8.35422 16.8214L13.2143 21.75L24.6458 10.25" stroke="none" stroke-width="3.5" stroke-linejoin="round" fill="none"></path></svg><span>Protected</span></label></li><li class="tsd-filter-item"><label class="tsd-filter-input"><input type="checkbox" id="tsd-filter-private" name="private"/><svg width="32" height="32" viewBox="0 0 32 32" aria-hidden="true"><rect class="tsd-checkbox-background" width="30" height="30" x="1" y="1" rx="6" fill="none"></rect><path class="tsd-checkbox-checkmark" d="M8.35422 16.8214L13.2143 21.75L24.6458 10.25" stroke="none" stroke-width="3.5" stroke-linejoin="round" fill="none"></path></svg><span>Private</span></label></li><li class="tsd-filter-item"><label class="tsd-filter-input"><input type="checkbox" id="tsd-filter-inherited" name="inherited" checked/><svg width="32" height="32" viewBox="0 0 32 32" aria-hidden="true"><rect class="tsd-checkbox-background" width="30" height="30" x="1" y="1" rx="6" fill="none"></rect><path class="tsd-checkbox-checkmark" d="M8.35422 16.8214L13.2143 21.75L24.6458 10.25" stroke="none" stroke-width="3.5" stroke-linejoin="round" fill="none"></path></svg><span>Inherited</span></label></li><li class="tsd-filter-item"><label class="tsd-filter-input"><input type="checkbox" id="tsd-filter-external" name="external"/><svg width="32" height="32" viewBox="0 0 32 32" aria-hidden="true"><rect class="tsd-checkbox-background" width="30" height="30" x="1" y="1" rx="6" fill="none"></rect><path class="tsd-checkbox-checkmark" d="M8.35422 16.8214L13.2143 21.75L24.6458 10.25" stroke="none" stroke-width="3.5" stroke-linejoin="round" fill="none"></path></svg><span>External</span></label></li></ul></div><div class="tsd-theme-toggle"><label class="settings-label" for="tsd-theme">Theme</label><select id="tsd-theme"><option value="os">OS</option><option value="light">Light</option><option value="dark">Dark</option></select></div></div></details></div><details open class="tsd-accordion tsd-page-navigation"><summary class="tsd-accordion-summary"><svg width="20" height="20" viewBox="0 0 24 24" fill="none" aria-hidden="true"><use href="assets/icons.svg#icon-chevronDown"></use></svg><h3>On This Page</h3></summary><div class="tsd-accordion-details"><a href="#async-wait-until"><span>async-<wbr/>wait-<wbr/>until</span></a><ul><li><a href="#๐Ÿ“–-detailed-documentation"><span>๐Ÿ“– <wbr/>Detailed <wbr/>Documentation</span></a></li><li><a href="#๐Ÿš€-installation"><span>๐Ÿš€ <wbr/>Installation</span></a></li><li><a href="#๐Ÿ› ๏ธ-how-to-use"><span>๐Ÿ› ๏ธ <wbr/>How to <wbr/>Use</span></a></li><li><ul><li><a href="#basic-example-wait-for-a-dom-element"><span>Basic <wbr/>Example: <wbr/>Wait for a DOM <wbr/>Element</span></a></li><li><a href="#handling-timeouts"><span>Handling <wbr/>Timeouts</span></a></li></ul></li><li><a href="#๐Ÿ“š-api"><span>๐Ÿ“š API</span></a></li><li><ul><li><a href="#waituntilpredicate-options"><span>wait<wbr/>Until(predicate, options)</span></a></li></ul></li><li><a href="#๐Ÿ’ก-recipes"><span>๐Ÿ’ก <wbr/>Recipes</span></a></li><li><ul><li><a href="#wait-indefinitely"><span>Wait <wbr/>Indefinitely</span></a></li><li><a href="#adjust-retry-interval"><span>Adjust <wbr/>Retry <wbr/>Interval</span></a></li></ul></li><li><a href="#๐Ÿงช-development-and-testing"><span>๐Ÿงช <wbr/>Development and <wbr/>Testing</span></a></li><li><a href="#๐Ÿ“-links"><span>๐Ÿ“ <wbr/>Links</span></a></li></ul></div></details></div><div class="site-menu"><nav class="tsd-navigation"><a href="modules.html">Documentation for async-wait-until v2.0.27</a><ul class="tsd-small-nested-navigation" id="tsd-nav-container"><li>Loading...</li></ul></nav></div></div></div><footer><p class="tsd-generator">Generated using <a href="https://typedoc.org/" target="_blank">TypeDoc</a></p></footer><div class="overlay"></div></body></html>