/*
* Tests the "whilst" function
*/
var mod_util = require('util');
var mod_tap = require('tap');
var mod_vasync = require('..');
mod_tap.test('basic whilst', function (test) {
var n = 0;
mod_vasync.whilst(
function condition() {
return (n < 5);
},
function body(cb) {
n++;
cb(null, n);
},
function done(err, arg) {
test.ok(!err, 'error unset');
test.equal(n, 5, 'n == 5');
test.equal(n, arg, 'n == arg');
test.end();
});
});
mod_tap.test('whilst return object', function (test) {
var n = 0;
var w = mod_vasync.whilst(
function condition() {
return (n < 5);
},
function body(cb) {
n++;
test.equal(n, w.iterations, 'n == w.iterations: ' + n);
cb(null, n, 'foo');
},
function done(err, arg1, arg2, arg3) {
test.ok(!err, 'error unset');
test.equal(w.iterations, 5, 'whilst had 5 iterations');
test.equal(w.finished, true, 'whilst has finished');
test.equal(arg1, n, 'whilst arg1 == n');
test.equal(arg2, 'foo', 'whilst arg2 == "foo"');
test.equal(arg3, undefined, 'whilst arg3 == undefined');
test.end();
});
test.equal(typeof (w), 'object', 'whilst returns an object');
test.equal(w.finished, false, 'whilst is not finished');
test.equal(w.iterations, 0, 'whilst has not started yet');
});
mod_tap.test('whilst false condition', function (test) {
mod_vasync.whilst(
function condition() {
return (false);
},
function body(cb) {
cb();
},
function done(err, arg) {
test.ok(!err, 'error is unset');
test.ok(!arg, 'arg is unset');
test.end();
});
});
mod_tap.test('whilst error', function (test) {
var n = 0;
var w = mod_vasync.whilst(
function condition() {
return (true);
},
function body(cb) {
n++;
if (n > 5) {
cb(new Error('n > 5'), 'bar');
} else {
cb(null, 'foo');
}
},
function done(err, arg) {
test.ok(err, 'error is set');
test.equal(err.message, 'n > 5');
test.equal(arg, 'bar');
test.equal(w.finished, true, 'whilst is finished');
/*
* Iterations is bumped after the test condition is run and
* before the iteration function is run. Because the condition
* in this example is inside the iteration function (the test
* condition always returns true), the iteration count will be
* 1 higher than expected, since it will fail when (n > 5), or
* when iterations is 6.
*/
test.equal(w.iterations, 6, 'whilst had 6 iterations');
test.end();
});
});
'use strict'
var painless = require('../../assertion/painless')
var test = painless.createGroup('Test promise/promtie::whilst')
var t = painless.assert
var whilst = require('../../../src/promise/promtie/whilst')
test('whilst(conditionFn, fn)', () => {
let count = 0;
let lastCount = count;
setInterval(() => {
count += 1;
}, 20);
return whilst(() => {
lastCount = count;
return count <= 3;
}, () => t.truthy(count <= 3))
.then(() => t.truthy(lastCount > 3));
});
test('whilst(conditionFn, fn, options)', () => {
const start = Date.now();
let count = 0;
let lastCount = count;
setInterval(() => {
count += 1;
}, 20);
return whilst(() => {
lastCount = count;
return count <= 3;
}, () => t.truthy(count <= 3), { delay: 125 })
.then(() => {
t.truthy(lastCount > 3);
t.truthy((Date.now() - start) > 100);
});
});
test('whilst(conditionFn, fn): conditionFn returns promise', () => {
let count = 0;
let lastCount = count;
setInterval(() => {
count += 1;
}, 20);
return whilst(() => {
lastCount = count;
return Promise.resolve(count <= 3);
}, () => t.truthy(count <= 3))
.then(() => t.truthy(lastCount > 3));
});
test('whilst(conditionFn, fn): fn returns promise', () => {
let count = 0;
let lastCount = count;
setInterval(() => {
count += 1;
}, 20);
return whilst(() => {
lastCount = count;
return count <= 3;
}, () => {
t.truthy(count <= 3);
return Promise.resolve();
})
.then(() => t.truthy(lastCount > 3));
});
test('whilst(conditionFn, fn): conditionFn and fn return promises', () => {
let count = 0;
let lastCount = count;
setInterval(() => {
count += 1;
}, 20);
return whilst(() => {
lastCount = count;
return Promise.resolve(count <= 3);
}, () => {
t.truthy(count <= 3);
return Promise.resolve();
})
.then(() => t.truthy(lastCount > 3));
});
test('whilst(conditionFn, fn): conditionFn throws', () => {
return whilst(() => {
throw new Error('condition not met'); }, () => t.fail('should not have run this'))
.then(() => t.fail('expected to fail'), err => t.is(err.message, 'condition not met'));
});
test('whilst(conditionFn, fn): fn throws', () => {
return whilst(() => true, () => {
throw new Error('condition not met'); })
.then(() => t.fail('expected to fail'), err => t.is(err.message, 'condition not met'));
});
'use strict';
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.default = doWhilst;
var _noop = require('lodash/noop');
var _noop2 = _interopRequireDefault(_noop);
var _slice = require('./internal/slice');
var _slice2 = _interopRequireDefault(_slice);
var _onlyOnce = require('./internal/onlyOnce');
var _onlyOnce2 = _interopRequireDefault(_onlyOnce);
var _wrapAsync = require('./internal/wrapAsync');
var _wrapAsync2 = _interopRequireDefault(_wrapAsync);
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
/**
* The post-check version of [`whilst`]{@link module:ControlFlow.whilst}. To reflect the difference in
* the order of operations, the arguments `test` and `iteratee` are switched.
*
* `doWhilst` is to `whilst` as `do while` is to `while` in plain JavaScript.
*
* @name doWhilst
* @static
* @memberOf module:ControlFlow
* @method
* @see [async.whilst]{@link module:ControlFlow.whilst}
* @category Control Flow
* @param {AsyncFunction} iteratee - A function which is called each time `test`
* passes. Invoked with (callback).
* @param {Function} test - synchronous truth test to perform after each
* execution of `iteratee`. Invoked with any non-error callback results of
* `iteratee`.
* @param {Function} [callback] - A callback which is called after the test
* function has failed and repeated execution of `iteratee` has stopped.
* `callback` will be passed an error and any arguments passed to the final
* `iteratee`'s callback. Invoked with (err, [results]);
*/
function doWhilst(iteratee, test, callback) {
callback = (0, _onlyOnce2.default)(callback || _noop2.default);
var _iteratee = (0, _wrapAsync2.default)(iteratee);
var next = function (err /*, ...args*/) {
if (err) return callback(err);
var args = (0, _slice2.default)(arguments, 1);
if (test.apply(this, args)) return _iteratee(next);
callback.apply(null, [null].concat(args));
};
_iteratee(next);
}
module.exports = exports['default'];