| 1 | 'use strict';
|
|---|
| 2 |
|
|---|
| 3 | Object.defineProperty(exports, "__esModule", {
|
|---|
| 4 | value: true
|
|---|
| 5 | });
|
|---|
| 6 |
|
|---|
| 7 | var _onlyOnce = require('./internal/onlyOnce.js');
|
|---|
| 8 |
|
|---|
| 9 | var _onlyOnce2 = _interopRequireDefault(_onlyOnce);
|
|---|
| 10 |
|
|---|
| 11 | var _wrapAsync = require('./internal/wrapAsync.js');
|
|---|
| 12 |
|
|---|
| 13 | var _wrapAsync2 = _interopRequireDefault(_wrapAsync);
|
|---|
| 14 |
|
|---|
| 15 | var _awaitify = require('./internal/awaitify.js');
|
|---|
| 16 |
|
|---|
| 17 | var _awaitify2 = _interopRequireDefault(_awaitify);
|
|---|
| 18 |
|
|---|
| 19 | function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
|
|---|
| 20 |
|
|---|
| 21 | /**
|
|---|
| 22 | * Repeatedly call `iteratee`, while `test` returns `true`. Calls `callback` when
|
|---|
| 23 | * stopped, or an error occurs.
|
|---|
| 24 | *
|
|---|
| 25 | * @name whilst
|
|---|
| 26 | * @static
|
|---|
| 27 | * @memberOf module:ControlFlow
|
|---|
| 28 | * @method
|
|---|
| 29 | * @category Control Flow
|
|---|
| 30 | * @param {AsyncFunction} test - asynchronous truth test to perform before each
|
|---|
| 31 | * execution of `iteratee`. Invoked with (callback).
|
|---|
| 32 | * @param {AsyncFunction} iteratee - An async function which is called each time
|
|---|
| 33 | * `test` passes. Invoked with (callback).
|
|---|
| 34 | * @param {Function} [callback] - A callback which is called after the test
|
|---|
| 35 | * function has failed and repeated execution of `iteratee` has stopped. `callback`
|
|---|
| 36 | * will be passed an error and any arguments passed to the final `iteratee`'s
|
|---|
| 37 | * callback. Invoked with (err, [results]);
|
|---|
| 38 | * @returns {Promise} a promise, if no callback is passed
|
|---|
| 39 | * @example
|
|---|
| 40 | *
|
|---|
| 41 | * var count = 0;
|
|---|
| 42 | * async.whilst(
|
|---|
| 43 | * function test(cb) { cb(null, count < 5); },
|
|---|
| 44 | * function iter(callback) {
|
|---|
| 45 | * count++;
|
|---|
| 46 | * setTimeout(function() {
|
|---|
| 47 | * callback(null, count);
|
|---|
| 48 | * }, 1000);
|
|---|
| 49 | * },
|
|---|
| 50 | * function (err, n) {
|
|---|
| 51 | * // 5 seconds have passed, n = 5
|
|---|
| 52 | * }
|
|---|
| 53 | * );
|
|---|
| 54 | */
|
|---|
| 55 | function whilst(test, iteratee, callback) {
|
|---|
| 56 | callback = (0, _onlyOnce2.default)(callback);
|
|---|
| 57 | var _fn = (0, _wrapAsync2.default)(iteratee);
|
|---|
| 58 | var _test = (0, _wrapAsync2.default)(test);
|
|---|
| 59 | var results = [];
|
|---|
| 60 |
|
|---|
| 61 | function next(err, ...rest) {
|
|---|
| 62 | if (err) return callback(err);
|
|---|
| 63 | results = rest;
|
|---|
| 64 | if (err === false) return;
|
|---|
| 65 | _test(check);
|
|---|
| 66 | }
|
|---|
| 67 |
|
|---|
| 68 | function check(err, truth) {
|
|---|
| 69 | if (err) return callback(err);
|
|---|
| 70 | if (err === false) return;
|
|---|
| 71 | if (!truth) return callback(null, ...results);
|
|---|
| 72 | _fn(next);
|
|---|
| 73 | }
|
|---|
| 74 |
|
|---|
| 75 | return _test(check);
|
|---|
| 76 | }
|
|---|
| 77 | exports.default = (0, _awaitify2.default)(whilst, 3);
|
|---|
| 78 | module.exports = exports.default; |
|---|