Projekt

Obecné

Profil

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

    
3
Object.defineProperty(exports, "__esModule", {
4
    value: true
5
});
6
exports.default = whilst;
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
 * Repeatedly call `iteratee`, while `test` returns `true`. Calls `callback` when
28
 * stopped, or an error occurs.
29
 *
30
 * @name whilst
31
 * @static
32
 * @memberOf module:ControlFlow
33
 * @method
34
 * @category Control Flow
35
 * @param {Function} test - synchronous truth test to perform before each
36
 * execution of `iteratee`. Invoked with ().
37
 * @param {AsyncFunction} iteratee - An async function which is called each time
38
 * `test` passes. Invoked with (callback).
39
 * @param {Function} [callback] - A callback which is called after the test
40
 * function has failed and repeated execution of `iteratee` has stopped. `callback`
41
 * will be passed an error and any arguments passed to the final `iteratee`'s
42
 * callback. Invoked with (err, [results]);
43
 * @returns undefined
44
 * @example
45
 *
46
 * var count = 0;
47
 * async.whilst(
48
 *     function() { return count < 5; },
49
 *     function(callback) {
50
 *         count++;
51
 *         setTimeout(function() {
52
 *             callback(null, count);
53
 *         }, 1000);
54
 *     },
55
 *     function (err, n) {
56
 *         // 5 seconds have passed, n = 5
57
 *     }
58
 * );
59
 */
60
function whilst(test, iteratee, callback) {
61
    callback = (0, _onlyOnce2.default)(callback || _noop2.default);
62
    var _iteratee = (0, _wrapAsync2.default)(iteratee);
63
    if (!test()) return callback(null);
64
    var next = function (err /*, ...args*/) {
65
        if (err) return callback(err);
66
        if (test()) return _iteratee(next);
67
        var args = (0, _slice2.default)(arguments, 1);
68
        callback.apply(null, [null].concat(args));
69
    };
70
    _iteratee(next);
71
}
72
module.exports = exports['default'];
(104-104/105)