-
Notifications
You must be signed in to change notification settings - Fork 5
/
index.js
38 lines (37 loc) · 899 Bytes
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
const repeat = require('apr-engine-repeat');
/**
* <a id="until"></a>
* Repeatedly call `fn` until `test` returns `true`.
*
* [![](https://img.shields.io/npm/v/apr-until.svg?style=flat-square)](https://www.npmjs.com/package/apr-until) [![](https://img.shields.io/npm/l/apr-until.svg?style=flat-square)](https://www.npmjs.com/package/apr-until)
*
* @kind function
* @name until
* @param {Function} test
* @param {Function} fn
* @returns {Promise}
*
* @example
* import until from 'apr-until';
*
* const then = (v) => new Promise((resolve) => resolve(v));
*
* const maxCalls = 10;
* let calls = 0;
*
*
* const output = await until(async () => {
* await then();
* return (calls += 1) >= maxCalls;
* }, async () => (
* await then(calls)
* );
*
* // output = 10
*/
module.exports = (test, fn) =>
repeat({
test,
fn,
after: tested => Boolean(tested)
});