summaryrefslogtreecommitdiff
path: root/lib/whilst.js
blob: 9406cd4eaae2f169c03aaad86ff9aa99fae16379 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
'use strict';

import noop from 'lodash/noop';
import rest from 'lodash/rest';

export default function whilst(test, iteratee, cb) {
    cb = cb || noop;
    if (!test()) return cb(null);
    var next = rest(function(err, args) {
        if (err) return cb(err);
        if (test.apply(this, args)) return iteratee(next);
        cb.apply(null, [null].concat(args));
    });
    iteratee(next);
}