summaryrefslogtreecommitdiff
path: root/lib/during.js
blob: 074d0daea6ca2c6782a79a04e4f9fc7ff9de4e9c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
'use strict';

import noop from 'lodash/utility/noop';
import restParam from 'lodash/function/restParam';

export default function during(test, iterator, cb) {
    cb = cb || noop;

    var next = restParam(function(err, args) {
        if (err) {
            cb(err);
        } else {
            args.push(check);
            test.apply(this, args);
        }
    });

    var check = function(err, truth) {
        if (err) return cb(err);
        if (!truth) return cb(null);
        iterator(next);
    };

    test(check);
}