root / HServer / 00.Server / 00.Program / node_modules / async / whilst.js
이력 | 보기 | 이력해설 | 다운로드 (2.04 KB)
1 | 39 | HKM | '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 _rest = require('./internal/rest'); |
||
13 | |||
14 | var _rest2 = _interopRequireDefault(_rest);
|
||
15 | |||
16 | var _onlyOnce = require('./internal/onlyOnce'); |
||
17 | |||
18 | var _onlyOnce2 = _interopRequireDefault(_onlyOnce);
|
||
19 | |||
20 | function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } |
||
21 | |||
22 | /**
|
||
23 | * Repeatedly call `iteratee`, while `test` returns `true`. Calls `callback` when
|
||
24 | * stopped, or an error occurs.
|
||
25 | *
|
||
26 | * @name whilst
|
||
27 | * @static
|
||
28 | * @memberOf module:ControlFlow
|
||
29 | * @method
|
||
30 | * @category Control Flow
|
||
31 | * @param {Function} test - synchronous truth test to perform before each
|
||
32 | * execution of `iteratee`. Invoked with ().
|
||
33 | * @param {Function} iteratee - A function which is called each time `test` passes.
|
||
34 | * The function is passed a `callback(err)`, which must be called once it has
|
||
35 | * completed with an optional `err` argument. Invoked with (callback).
|
||
36 | * @param {Function} [callback] - A callback which is called after the test
|
||
37 | * function has failed and repeated execution of `iteratee` has stopped. `callback`
|
||
38 | * will be passed an error and any arguments passed to the final `iteratee`'s
|
||
39 | * callback. Invoked with (err, [results]);
|
||
40 | * @returns undefined
|
||
41 | * @example
|
||
42 | *
|
||
43 | * var count = 0;
|
||
44 | * async.whilst(
|
||
45 | * function() { return count < 5; },
|
||
46 | * function(callback) {
|
||
47 | * count++;
|
||
48 | * setTimeout(function() {
|
||
49 | * callback(null, count);
|
||
50 | * }, 1000);
|
||
51 | * },
|
||
52 | * function (err, n) {
|
||
53 | * // 5 seconds have passed, n = 5
|
||
54 | * }
|
||
55 | * );
|
||
56 | */
|
||
57 | function whilst(test, iteratee, callback) { |
||
58 | callback = (0, _onlyOnce2.default)(callback || _noop2.default); |
||
59 | if (!test()) return callback(null); |
||
60 | var next = (0, _rest2.default)(function (err, args) { |
||
61 | if (err) return callback(err); |
||
62 | if (test()) return iteratee(next); |
||
63 | callback.apply(null, [null].concat(args)); |
||
64 | }); |
||
65 | iteratee(next); |
||
66 | } |
||
67 | module.exports = exports['default']; |