npm

Share private packages across your team with npm Orgs, now with simplified billing via the aws marketplace!Learn more »

wait-promise

0.4.1 • Public • Published

wait-promise

npm status build status dependency status coverage status

Make a promise, waiting for a specified amount of time, util something is done. Syntactic sugar for setTimeout and setInterval. Based on ES6 Promise.

Use it in nodeJS

A version compiled to ES5 in CJS format is published to npm as wait-promise.

npm install wait-promise

in ES5:

var wait = require('wait-promise');

in ES6/7:

import {check, until, till, before, after, limit, every, and, sleep} from 'wait-promise';

Use it on browser

wait-promise CDN

<script src="https://s5.ssl.qhimg.com/!e29d1180/wait-promise.min.js"></script>

You can use it with any AMD loader or standalone

var promise = wait.before(5000).until(function(){
  return typeof Babel !== 'undefined';
});
 
promise.then(function(){
  code = Babel.transform(code, { presets: ['es2015-loose', 'react', 'stage-0'] }).code;
}).catch(function(){
  console.error('Cannot load babeljs.');
});

API Doc

check

wait.check(condition) always returns a promise. If condition throws error or returns false value explicitly, the promise will be rejected.

let i = 1;
let promise = wait.check(function(){
  return i < 1;
});
promise.catch(function(err){
  console.log(err.message); //will be check failed
});

until

wait.until(condition)

Check condition async and re-check every 100ms until it neighter throws error nor returns false.

let i = 0;
let promise = wait.until(function(){
    return ++>= 10;
});
promise.then(function(){
    console.log(i); //i will be 10
});

With async/wait

let until = wait.until;
 
async function foo(){
    let i = 0;
    
    await until(() => ++> 10);
    bar();
}

till

wait.till(condition)

Check condition async and re-check every 100ms till it explicitly returns true. If condition throws error, the promise will be rejected.

let i = 0;
let promise = wait.till(function(){
  return ++>= 10;
});
promise.then(function(){
  console.log(i); //i will be 10
});

before

wait.before(millisec).until(condition)

Check condition in millisec. If time out, the promise will be rejected.

let i = 0;
let promise = wait.before(200).until(function(){
    return ++>= 10;
});
promise.catch(function(err){
    console.log(i, err.message); //2 check failed
});

after

wait.after(millisec).check(condition)

Check condition after millisec.

let i = 1;
setTimeout(function(){
    ++i;
}, 0); 
let promise = wait.after(1000).check(function(){
    return i > 200;
});

limit

Check condition with limit times. If exceed the limit, the promise will be rejected.

let i = 0;
let p = wait.limit(10).until(function(){
  i++;
  return false;
});
return p.catch(function(){
  console.log(i); //i will be 10
});

every

wait.every(millisec[,limit]).until(condition)

Change time interval from 100ms to millisec

let i = 0;
let promise = wait.every(10).before(200).until(function(){
    return ++>= 10;
});
promise.then(function(){
    console.log(i) // i will be 10
});

every with limit

wait.every(1, 10) is equal to wait.every(1).limit(10)

let i = 0;
let p = wait.every(1, 10).till(function(){
  return ++>= 10;
});
p.catch(function(){
  console.log(i); //i will be 10
});

and

wait.every(millisec).and(func).until(condition)

Check every millisec time and do something before checking condition.

async function foo(){
  let i = 0, j = 0;
  await and(() => j++).until(() => j >= 3);
  await every(50).and(() => i++).until(()=> i >= 5);
  console.log(+ j); //will be 8
}

sleep

wait.sleep(millisec)

Do nothing but sleep millisec

var promise = wait.sleep(200);
promise.then(function(){
    //do sth.
});

With async/await

let sleep = wait.sleep;
 
async function foo(){
    await sleep(500);
    bar();
} 

LICENSE

MIT

install

npm i wait-promise

Downloadsweekly downloads

5,263

version

0.4.1

license

MIT

last publish

collaborators

  • avatar
Report a vulnerability