1.8.0 • Published 3 years ago

wait-for-cond v1.8.0

Weekly downloads
5
License
ISC
Repository
github
Last release
3 years ago

wait-for-cond

Wait until a condition is satisfied. Useful for testing.

Installation

$ npm i -S wait-for-cond

Usage

The promise resolves if the condition is met at least once in the specified duration, and rejects otherwise.

var waitFor = require('wait-for-cond');

var someCondition = true;

waitFor(function() {
    return someCondition;
}, 2000, 'an optional reject message')
.then(function() {
    console.log('condition is fulfilled.');
})
.catch(function() {
    console.error('condition was not fulfilled in time.');
});

The condition result can also be wrapped in a promise.

var waitFor = require('wait-for-cond');

var someCondition = true;

waitFor(function() {
    return new Promise(function(resolve) {
        return resolve(someCondition);
    });
}, 2000, 'an optional reject message')
.then(function() {
    console.log('condition is fulfilled.');
})
.catch(function() {
    console.error('condition was not fulfilled in time.');
});

waitFor.hold resolves if the condition remains satisfied for the entire duration, and rejects otherwise.

var waitFor = require('wait-for-cond');

var someCondition = true;

waitFor.hold(function() {
    return new Promise(function(resolve) {
        return someCondition;
    });
}, 2000, 'an optional reject message')
.then(function() {
    console.log('condition remained satisfied for 2000ms');
})
.catch(function() {
    console.error('condition was unsatisfied during the 2000ms duration');
});

Eventual assertions

The promise resolves if the assertion was fulfilled at least once in the specified duration, and rejects otherwise.

var assert = require('assert');
var waitFor = require('wait-for-cond');

var someCondition = true;

waitFor.assert(function() {
    assert(someCondition);
}, 2000)
.then(function() {
    console.log('assertion succeeded in time.');
})
.catch(function() {
    console.log('assertion did not succeed in time.');
});

Eventual holding assertions

The promise resolves if the assertion remains fulfilled for the entire specified duration, and rejects otherwise.

var assert = require('assert');
var waitFor = require('wait-for-cond');

var someCondition = true;

waitFor.assertHold(function() {
    assert(someCondition);
}, 2000)
.then(function() {
    console.log('assertion was held for the entire duration.');
})
.catch(function() {
    console.log('assertion failed in the specified duration.');
});
1.8.0

3 years ago

1.7.0

5 years ago

1.6.0

5 years ago

1.5.1

7 years ago

1.5.0

7 years ago

1.4.1

7 years ago

1.4.0

7 years ago

1.3.0

7 years ago

1.2.0

7 years ago

1.1.0

7 years ago

1.0.3

7 years ago

1.0.2

7 years ago

1.0.1

7 years ago

1.0.0

7 years ago