1 | 'use strict';
|
---|
2 |
|
---|
3 | Object.defineProperty(exports, "__esModule", {
|
---|
4 | value: true
|
---|
5 | });
|
---|
6 | exports.default = during;
|
---|
7 |
|
---|
8 | var _noop = require('lodash/noop');
|
---|
9 |
|
---|
10 | var _noop2 = _interopRequireDefault(_noop);
|
---|
11 |
|
---|
12 | var _onlyOnce = require('./internal/onlyOnce');
|
---|
13 |
|
---|
14 | var _onlyOnce2 = _interopRequireDefault(_onlyOnce);
|
---|
15 |
|
---|
16 | var _wrapAsync = require('./internal/wrapAsync');
|
---|
17 |
|
---|
18 | var _wrapAsync2 = _interopRequireDefault(_wrapAsync);
|
---|
19 |
|
---|
20 | function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
|
---|
21 |
|
---|
22 | /**
|
---|
23 | * Like [`whilst`]{@link module:ControlFlow.whilst}, except the `test` is an asynchronous function that
|
---|
24 | * is passed a callback in the form of `function (err, truth)`. If error is
|
---|
25 | * passed to `test` or `fn`, the main callback is immediately called with the
|
---|
26 | * value of the error.
|
---|
27 | *
|
---|
28 | * @name during
|
---|
29 | * @static
|
---|
30 | * @memberOf module:ControlFlow
|
---|
31 | * @method
|
---|
32 | * @see [async.whilst]{@link module:ControlFlow.whilst}
|
---|
33 | * @category Control Flow
|
---|
34 | * @param {AsyncFunction} test - asynchronous truth test to perform before each
|
---|
35 | * execution of `fn`. Invoked with (callback).
|
---|
36 | * @param {AsyncFunction} fn - An async function which is called each time
|
---|
37 | * `test` passes. Invoked with (callback).
|
---|
38 | * @param {Function} [callback] - A callback which is called after the test
|
---|
39 | * function has failed and repeated execution of `fn` has stopped. `callback`
|
---|
40 | * will be passed an error, if one occurred, otherwise `null`.
|
---|
41 | * @example
|
---|
42 | *
|
---|
43 | * var count = 0;
|
---|
44 | *
|
---|
45 | * async.during(
|
---|
46 | * function (callback) {
|
---|
47 | * return callback(null, count < 5);
|
---|
48 | * },
|
---|
49 | * function (callback) {
|
---|
50 | * count++;
|
---|
51 | * setTimeout(callback, 1000);
|
---|
52 | * },
|
---|
53 | * function (err) {
|
---|
54 | * // 5 seconds have passed
|
---|
55 | * }
|
---|
56 | * );
|
---|
57 | */
|
---|
58 | function during(test, fn, callback) {
|
---|
59 | callback = (0, _onlyOnce2.default)(callback || _noop2.default);
|
---|
60 | var _fn = (0, _wrapAsync2.default)(fn);
|
---|
61 | var _test = (0, _wrapAsync2.default)(test);
|
---|
62 |
|
---|
63 | function next(err) {
|
---|
64 | if (err) return callback(err);
|
---|
65 | _test(check);
|
---|
66 | }
|
---|
67 |
|
---|
68 | function check(err, truth) {
|
---|
69 | if (err) return callback(err);
|
---|
70 | if (!truth) return callback(null);
|
---|
71 | _fn(next);
|
---|
72 | }
|
---|
73 |
|
---|
74 | _test(check);
|
---|
75 | }
|
---|
76 | module.exports = exports['default']; |
---|