sg-until

1.0.0 • Public • Published

TOC

sg-until

continuously calls a function every 100ms until it returns truthy.

var counter = 0;
until( function () {
    counter++;
    if ( counter === 5 ) return 'chicken';
}, function ( _error, _result ) {
    should( _error ).equal( null );
    _result.should.eql( 'chicken' );
    _done();
} );

should wait for about 3 seconds.

var started = new Date().getTime(),
    timesChecked = 0;
until( function () {
    timesChecked++;
    return new Date().getTime() - started > 500;
}, function () {
    ( new Date().getTime() - started ).should.be.greaterThan( 500 );
    timesChecked.should.be.greaterThan( ( 500 / 100 * .8 ) );
    _done();
} );

should wait for about 3 seconds but check every 10 milliseconds.

var started = new Date().getTime(),
    timesChecked = 0;
until( function () {
    timesChecked++;
    return new Date().getTime() - started > 500;
}, function () {
    ( new Date().getTime() - started ).should.be.greaterThan( 500 );
    timesChecked.should.be.greaterThan( 500 / 10 * .8 );
    _done();
}, 10 );

should stop checking if an error is returned.

var timesChecked = 0;
until( function () {
    timesChecked++;
    if ( timesChecked === 5 ) return new Error( '5 is odd' );
}, function ( _error ) {
    _error.should.be.an.Error;
    _done();
} );

should timeout if specified.

var timeout = 1000;
until( function () {
    return false;
}, function ( _error ) {
    _error.should.be.an.Error;
    /timed out/i.test( _error.message );
    _done();
}, 100, timeout );

Readme

Keywords

none

Package Sidebar

Install

npm i sg-until

Weekly Downloads

4

Version

1.0.0

License

MIT

Last publish

Collaborators

  • davidtsuji