npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

wait-promise

v0.4.1

Published

Make a promise, waiting for a specified amount of time, util something is done.

Downloads

22,231

Readme

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 ++i >= 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(() => ++i > 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 ++i >= 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 ++i >= 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 ++i >= 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 ++i >= 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(i + 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