; | |
Object.defineProperty(exports, "__esModule", { | |
value: true | |
}); | |
var _onlyOnce = require('./internal/onlyOnce.js'); | |
var _onlyOnce2 = _interopRequireDefault(_onlyOnce); | |
var _wrapAsync = require('./internal/wrapAsync.js'); | |
var _wrapAsync2 = _interopRequireDefault(_wrapAsync); | |
var _awaitify = require('./internal/awaitify.js'); | |
var _awaitify2 = _interopRequireDefault(_awaitify); | |
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } | |
/** | |
* Repeatedly call `iteratee`, while `test` returns `true`. Calls `callback` when | |
* stopped, or an error occurs. | |
* | |
* @name whilst | |
* @static | |
* @memberOf module:ControlFlow | |
* @method | |
* @category Control Flow | |
* @param {AsyncFunction} test - asynchronous truth test to perform before each | |
* execution of `iteratee`. Invoked with (callback). | |
* @param {AsyncFunction} iteratee - An async function which is called each time | |
* `test` passes. Invoked with (callback). | |
* @param {Function} [callback] - A callback which is called after the test | |
* function has failed and repeated execution of `iteratee` has stopped. `callback` | |
* will be passed an error and any arguments passed to the final `iteratee`'s | |
* callback. Invoked with (err, [results]); | |
* @returns {Promise} a promise, if no callback is passed | |
* @example | |
* | |
* var count = 0; | |
* async.whilst( | |
* function test(cb) { cb(null, count < 5); }, | |
* function iter(callback) { | |
* count++; | |
* setTimeout(function() { | |
* callback(null, count); | |
* }, 1000); | |
* }, | |
* function (err, n) { | |
* // 5 seconds have passed, n = 5 | |
* } | |
* ); | |
*/ | |
function whilst(test, iteratee, callback) { | |
callback = (0, _onlyOnce2.default)(callback); | |
var _fn = (0, _wrapAsync2.default)(iteratee); | |
var _test = (0, _wrapAsync2.default)(test); | |
var results = []; | |
function next(err, ...rest) { | |
if (err) return callback(err); | |
results = rest; | |
if (err === false) return; | |
_test(check); | |
} | |
function check(err, truth) { | |
if (err) return callback(err); | |
if (err === false) return; | |
if (!truth) return callback(null, ...results); | |
_fn(next); | |
} | |
return _test(check); | |
} | |
exports.default = (0, _awaitify2.default)(whilst, 3); | |
module.exports = exports.default; |