Projekt

Obecné

Profil

Stáhnout (2.02 KB) Statistiky
| Větev: | Revize:
1
'use strict';
2

    
3
Object.defineProperty(exports, "__esModule", {
4
    value: true
5
});
6
exports.default = doWhilst;
7

    
8
var _noop = require('lodash/noop');
9

    
10
var _noop2 = _interopRequireDefault(_noop);
11

    
12
var _slice = require('./internal/slice');
13

    
14
var _slice2 = _interopRequireDefault(_slice);
15

    
16
var _onlyOnce = require('./internal/onlyOnce');
17

    
18
var _onlyOnce2 = _interopRequireDefault(_onlyOnce);
19

    
20
var _wrapAsync = require('./internal/wrapAsync');
21

    
22
var _wrapAsync2 = _interopRequireDefault(_wrapAsync);
23

    
24
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
25

    
26
/**
27
 * The post-check version of [`whilst`]{@link module:ControlFlow.whilst}. To reflect the difference in
28
 * the order of operations, the arguments `test` and `iteratee` are switched.
29
 *
30
 * `doWhilst` is to `whilst` as `do while` is to `while` in plain JavaScript.
31
 *
32
 * @name doWhilst
33
 * @static
34
 * @memberOf module:ControlFlow
35
 * @method
36
 * @see [async.whilst]{@link module:ControlFlow.whilst}
37
 * @category Control Flow
38
 * @param {AsyncFunction} iteratee - A function which is called each time `test`
39
 * passes. Invoked with (callback).
40
 * @param {Function} test - synchronous truth test to perform after each
41
 * execution of `iteratee`. Invoked with any non-error callback results of
42
 * `iteratee`.
43
 * @param {Function} [callback] - A callback which is called after the test
44
 * function has failed and repeated execution of `iteratee` has stopped.
45
 * `callback` will be passed an error and any arguments passed to the final
46
 * `iteratee`'s callback. Invoked with (err, [results]);
47
 */
48
function doWhilst(iteratee, test, callback) {
49
    callback = (0, _onlyOnce2.default)(callback || _noop2.default);
50
    var _iteratee = (0, _wrapAsync2.default)(iteratee);
51
    var next = function (err /*, ...args*/) {
52
        if (err) return callback(err);
53
        var args = (0, _slice2.default)(arguments, 1);
54
        if (test.apply(this, args)) return _iteratee(next);
55
        callback.apply(null, [null].concat(args));
56
    };
57
    _iteratee(next);
58
}
59
module.exports = exports['default'];
(29-29/105)