# wait-promise

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

Latest version **0.4.1** (published 2016-07-27) · MIT license · 0 weekly downloads

## Install

```sh
npm install wait-promise
pnpm add wait-promise
yarn add wait-promise
bun add wait-promise
```

## Health

**Score 23/100 (F)** — status: abandoned.

Positive: has types package; no vulnerabilities; high quality score.

Warnings: low downloads; no esm support; pre 1.0.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 0.4.1 |
| Published | 2016-07-27 |
| First published | 2016-05-05 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | separate (@types/wait-promise) |
| Module format | CommonJS |
| Dependencies | 0 |
| Known vulnerabilities | 0 |
| Install scripts | no |
| Author | akira-cn |
| Maintainers | akira_cn |
| Keywords | promise, node, wait, before, after, every, and, until, till, async |

## Links

- npm: https://www.npmjs.com/package/wait-promise
- npm.io page: https://npm.io/package/wait-promise

## Alternatives

- [promise-throttle](https://npm.io/package/promise-throttle.md) — 44.2K weekly downloads
- [@commercetools/sync-actions](https://npm.io/package/@commercetools/sync-actions.md) — 25.1K weekly downloads
- [cwait](https://npm.io/package/cwait.md) — 21.4K weekly downloads
- [@ledgerhq/hw-app-cosmos](https://npm.io/package/@ledgerhq/hw-app-cosmos.md) — 4.2K weekly downloads
- [@financial-times/o-loading](https://npm.io/package/@financial-times/o-loading.md) — 2.8K weekly downloads

## Recent versions

- 0.4.1 (latest) — 2016-07-27
- 0.3.2 — 2016-07-26
- 0.3.1 — 2016-05-12
- 0.3.0 — 2016-05-12
- 0.2.0 — 2016-05-06
- 0.1.0 — 2016-05-05

## README

# wait-promise

[![npm status](https://img.shields.io/npm/v/wait-promise.svg)](https://www.npmjs.org/package/wait-promise)
[![build status](https://api.travis-ci.org/akira-cn/wait-promise.svg?branch=master)](https://travis-ci.org/akira-cn/wait-promise) 
[![dependency status](https://david-dm.org/akira-cn/wait-promise.svg)](https://david-dm.org/akira-cn/wait-promise) 
[![coverage status](https://img.shields.io/coveralls/akira-cn/wait-promise.svg)](https://coveralls.io/github/akira-cn/wait-promise)

Make a promise, waiting for a specified amount of time, util something is done. Syntactic sugar for setTimeout and setInterval. Based on [ES6 Promise](https://developer.mozilla.org/zh-CN/docs/Web/JavaScript/Reference/Global_Objects/Promise).

## Use it in nodeJS

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

```bash
npm install wait-promise
```

in ES5:

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

in ES6/7:

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

## Use it on browser

**wait-promise CDN**

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

You can use it with any AMD loader or **standalone**

```js
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](#check)
* [until](#until)
* [till](#till)
* [before](#before)
* [after](#after)
* [limit](#limit)
* [every](#every)
* [and](#and)
* [sleep](#sleep)

### check

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

```js
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`.

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

**With async/wait**

```js
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.

```js
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.

```js
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`.

```js
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.

```js
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`。

```js
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)` 

```js
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.

```js
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`

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

**With async/await**

```js
let sleep = wait.sleep;

async function foo(){
	await sleep(500);
	bar();
} 
```

## LICENSE

[MIT](LICENSE)

---
_Source: https://npm.io/package/wait-promise · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
