forked from Azure/login
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwhilst.js
More file actions
31 lines (30 loc) · 894 Bytes
/
Copy pathwhilst.js
File metadata and controls
31 lines (30 loc) · 894 Bytes
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
import until from './until'
/**
* Run `task` repeatedly until `test` returns `false`.
* Calls `callback` at the first error encountered.
*
* @name whilst
* @memberOf module:serial
* @static
* @method
* @param {Function} test - test function `function (index: number)`. If return value is `false` then `callback` gets called
* @param {Function} task - iterator function of type `function (cb: Function, index: Number)`
* @param {Function} [callback] - optional callback `function (errors: Error, result: any)` from last callback.
*
* @example
* let arr = []
* whilst(
* (index) => (index < 4), // test
* (cb, index) => { // task
* arr.push(index)
* cb(null, index)
* }, (err, res) => { // callback
* //> err = null
* //> res = 3
* //> arr = [0, 1, 2, 3]
* }
* )
*/
export default function whilst (test, task, callback) {
until((n) => (!test(n)), task, callback)
}