UNPKG

2.18 kBJavaScriptView Raw
1'use strict';
2
3Object.defineProperty(exports, "__esModule", {
4 value: true
5});
6exports.default = doDuring;
7
8var _noop = require('lodash/noop');
9
10var _noop2 = _interopRequireDefault(_noop);
11
12var _slice = require('./internal/slice');
13
14var _slice2 = _interopRequireDefault(_slice);
15
16var _onlyOnce = require('./internal/onlyOnce');
17
18var _onlyOnce2 = _interopRequireDefault(_onlyOnce);
19
20var _wrapAsync = require('./internal/wrapAsync');
21
22var _wrapAsync2 = _interopRequireDefault(_wrapAsync);
23
24function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
25
26/**
27 * The post-check version of [`during`]{@link module:ControlFlow.during}. To reflect the difference in
28 * the order of operations, the arguments `test` and `fn` are switched.
29 *
30 * Also a version of [`doWhilst`]{@link module:ControlFlow.doWhilst} with asynchronous `test` function.
31 * @name doDuring
32 * @static
33 * @memberOf module:ControlFlow
34 * @method
35 * @see [async.during]{@link module:ControlFlow.during}
36 * @category Control Flow
37 * @param {AsyncFunction} fn - An async function which is called each time
38 * `test` passes. Invoked with (callback).
39 * @param {AsyncFunction} test - asynchronous truth test to perform before each
40 * execution of `fn`. Invoked with (...args, callback), where `...args` are the
41 * non-error args from the previous callback of `fn`.
42 * @param {Function} [callback] - A callback which is called after the test
43 * function has failed and repeated execution of `fn` has stopped. `callback`
44 * will be passed an error if one occurred, otherwise `null`.
45 */
46function doDuring(fn, test, callback) {
47 callback = (0, _onlyOnce2.default)(callback || _noop2.default);
48 var _fn = (0, _wrapAsync2.default)(fn);
49 var _test = (0, _wrapAsync2.default)(test);
50
51 function next(err /*, ...args*/) {
52 if (err) return callback(err);
53 var args = (0, _slice2.default)(arguments, 1);
54 args.push(check);
55 _test.apply(this, args);
56 };
57
58 function check(err, truth) {
59 if (err) return callback(err);
60 if (!truth) return callback(null);
61 _fn(next);
62 }
63
64 check(null, true);
65}
66module.exports = exports['default'];
\No newline at end of file