mirror of
https://github.com/azure/login.git
synced 2026-06-06 18:17:07 +00:00
60 lines
No EOL
1.4 KiB
JavaScript
60 lines
No EOL
1.4 KiB
JavaScript
"use strict";
|
|
|
|
Object.defineProperty(exports, "__esModule", {
|
|
value: true
|
|
});
|
|
exports["default"] = until;
|
|
|
|
var _setImmediate2 = require("./_setImmediate");
|
|
|
|
/**
|
|
* Run `task` repeatedly until `test` returns `true`.
|
|
* Calls `callback` at the first error encountered.
|
|
*
|
|
* @name until
|
|
* @memberOf module:serial
|
|
* @static
|
|
* @method
|
|
* @param {Function} test - test function `function (index: number)`. If return value is `true` 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 = []
|
|
* until(
|
|
* (index) => { // test
|
|
* return index >= 4
|
|
* }, (cb, index) => { // task
|
|
* arr.push(index)
|
|
* cb(null, index)
|
|
* }, (err, res) => { // callback
|
|
* //> err = null
|
|
* //> res = 3
|
|
* //> arr = [0, 1, 2, 3]
|
|
* }
|
|
* )
|
|
*/
|
|
function until(test, task, callback) {
|
|
var i = 0;
|
|
|
|
function cb(err, res) {
|
|
if (err || test(i)) {
|
|
callback && callback(err, res);
|
|
} else {
|
|
(0, _setImmediate2._setImmediate)(function () {
|
|
// prevent RangeError: Maximum call stack size exceeded for sync tasks
|
|
run();
|
|
});
|
|
}
|
|
}
|
|
|
|
function run() {
|
|
task(cb, i++);
|
|
}
|
|
|
|
if (!test(i)) {
|
|
run();
|
|
} else {
|
|
callback && callback();
|
|
}
|
|
} |