1.0.0 • Published 6 years ago

node-wait-until v1.0.0

Weekly downloads
70
License
MIT
Repository
github
Last release
6 years ago

waitUntil

Build Status Coverage Status

Waits for a function result to be truthy. Keeps trying until it happens or timeout is reached.

Similar to async-wait-until but with support for functions that returns a Promise.

Installation

$ npm install node-wait-until

Usage

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

const startTime = Date.now();

function enoughTimeHasPassed() {
    const elapsedTime = Date.now() - startTime;

    return (elapsedTime > 500)
        ? 'some value'
        : false;
}

waitUntil(enoughTimeHasPassed, 600)
    .then(result => console.log(`Operation succeeded with result: ${result}`))
    .catch(error => console.log(`Operation failed with error: ${error}`));

API

waitUntil(function, timeout, delay)

Returns a Promise that resolves when function returns a truthy result.

Rejects if function throws or returns a Promise that rejects, or if timeout is reached.

function

Type: Function

Expected to return a truthy result or a Promise for a truthy result.

timeout

Type: Number Default: 5000

Number of milliseconds to wait before rejecting.

delay

Type: Number Default: 20

Number of milliseconds to wait before retrying function.

License

MIT © Zed-K