UNPKG

asyncc

Version:
39 lines (38 loc) 1.19 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports["default"] = doWhilst; var _doUntil = _interopRequireDefault(require("./doUntil")); function _interopRequireDefault(e) { return e && e.__esModule ? e : { "default": e }; } /** * Run `task` one or more times until `test` returns `false`. * Calls `callback` at the first error encountered. * * @name doWhilst * @memberOf module:serial * @static * @method * @param {Function} task - iterator function of type `function (cb: Function, index: Number)` * @param {Function} test - test function `function (index: number)`. If return value is `false` then `callback` gets called * @param {Function} [callback] - optional callback `function (errors: <Error>, result: any)` from last callback. * @example * let arr = [] * doWhilst( * (cb, index) => { // task * arr.push(index) * cb(null, index) * }, (index) => { // test * return index < 4 * }, (err, res) => { // callback * //> err = null * //> res = 3 * //> arr = [0, 1, 2, 3] * } * ) */ function doWhilst(task, test, callback) { (0, _doUntil["default"])(task, function (n) { return !test(n); }, callback); }