Sign Up for Free

RunKit +

Try any Node.js package right in your browser

This is a playground to test code. It runs a full Node.js environment and already has all of npm’s 1,000,000+ packages pre-installed, including async-wait-until with all npm packages installed. Try it out:

var asyncWaitUntil = require("async-wait-until")

This service is provided by RunKit and is not affiliated with npm, Inc or the package authors.

async-wait-until v1.2.6

Wait while predicate completes and resolve a Promise

async-wait-until

Waits for predicate to be truthy and resolves a Promise. The library provides TypeScript definitions. No dependencies needed. Please be sure that your environment has Promise support (or has a polyfill).

Build Status Test Coverage Maintainability npm version

Installation

$ npm install --save async-wait-until

Usage

The simple use case is described below:

const waitUntil = require('async-wait-until');

const timeOfStart = Date.now();


// Wait for some async operation to end
waitUntil(() => {
  const timePassed = Date.now() - timeOfStart;

  return (timePassed < 500)
      && (timePassed % 2 === 0)  // Some random stuff
          ? true
          : throw new Error('Async operation failed');
}, 600)
.then((result) => {
  // Here are the operations to be done after predicate
  console.log('Async operation succeeded, predicate result = ', result);
})
.catch((error) => {
  // Here are the operations to be done if predicate didn't succeed in the timeout
  console.log('Async operation failed: ', error);
});

async/await

The library is async/await compatible because it uses Promises/A+, so the example above could be rewritten:

const waitUntil = require('async-wait-until');

const timeOfStart = Date.now();


// Wait for some async operation to end
try {
  const result = await waitUntil(() => {
    const timePassed = Date.now() - timeOfStart;

    return (timePassed < 500)
        && (timePassed % 2 === 0)  // Some random stuff
            ? true
            : throw new Error('Async operation failed');
  }, 600);

  // Here are the operations to be done after predicate
  console.log('Async operation succeeded, predicate result = ', result);
} catch (error) {
  // Here are the operations to be done if predicate didn't succeed in the timeout
  console.log('Async operation failed: ', error);
}

Supported arguments

/**
 * Waits for predicate to be truthy and resolves a Promise
 *
 * @param  predicate  Function  Predicate that checks the condition
 * @param  timeout  Number  Maximum wait interval, optional, 5000ms by default
 * @param  interval  Number  Wait interval, optional, 50ms by default
 * @return  Promise  Promise to return a callback result
 */
function waitUntil(
    predicate: Function,
    timeout: number = 5000,
    interval: number = 50
): Promise;

TypeScript

The library exports type definitions for TypeScript. As far as the library ships the code with commonJS module support only, you have to use import * as syntax to use it:

import * as waitUntil from 'async-wait-until';

const timeOfStart = Date.now();


// Wait for some async operation to end
try {
  const result = await waitUntil(() => {
    const timePassed = Date.now() - timeOfStart;

    return (timePassed < 500)
        && (timePassed % 2 === 0)  // Some random stuff
            ? true
            : throw new Error('Async operation failed');
  }, 600);

  // Here are the operations to be done after predicate
  console.log('Async operation succeeded, predicate result = ', result);
} catch (error) {
  // Here are the operations to be done if predicate didn't succeed in the timeout
  console.log('Async operation failed: ', error);
}

Test coverage

Library has 100% test coverage:

$ npm run test:coverage

> async-wait-until@1.1.4 test:coverage ~/projects/waitUntil
> NODE_ENV=test jest --coverage --no-cache --config .jestrc

 PASS  test/waitUntil.js
  waitUntil
    ✓ Should apply callback and resolve result (219ms)
    ✓ Should apply callback and resolve non-boolean result (209ms)
    ✓ Should reject with timeout error if timed out (108ms)
    ✓ Should not do double reject on timeout (205ms)
    ✓ Should not do double reject on timeout if error in predicate (213ms)
    ✓ Should reject result if error in predicate (55ms)

--------------|----------|----------|----------|----------|----------------|
File          |  % Stmts | % Branch |  % Funcs |  % Lines |Uncovered Lines |
--------------|----------|----------|----------|----------|----------------|
All files     |      100 |      100 |      100 |      100 |                |
 waitUntil.js |      100 |      100 |      100 |      100 |                |
--------------|----------|----------|----------|----------|----------------|
Test Suites: 1 passed, 1 total
Tests:       6 passed, 6 total
Snapshots:   0 total
Time:        1.984s
Ran all test suites.

Code style

Library is 100% compatible with airbnb-base for ES5.

Available commands

Library has the following commands available:

  • Run the tests:

    $ npm test
    
  • Run the tests and display test coverage:

    $ npm run test:coverage
    
  • Run the linter:

    $ npm run lint
    

Build

No building required, library is implemented with ES5 for better compatibility and shipped as is.

License

Library is shipped "as is" under MIT License.

Contributing

Feel free to contribute. Just raise ann issue, create a pull request covering it and don't forget to test everything properly.

NPM

RunKit is a free, in-browser JavaScript dev environment for prototyping Node.js code, with every npm package installed. Sign up to share your code.
Sign Up for Free