'a'
mh-two-thousand-and-two
2024-04-12 44d2c92345cd156a59fc327b3060292a282d2893
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
26
27
28
29
30
31
32
33
34
35
36
var now = require('./now');
exports = function(condition) {
    var timeout =
        arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : 0;
    var interval =
        arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : 250;
    function evalCondition() {
        return new Promise(function(resolve, reject) {
            try {
                resolve(condition());
            } catch (e) {
                reject(e);
            }
        });
    }
    return new Promise(function(resolve, reject) {
        var startTime = now();
        var pollCondition = function() {
            evalCondition().then(function(val) {
                var elapsed = now() - startTime;
                if (val) {
                    resolve(val);
                } else if (timeout && elapsed >= timeout) {
                    reject(
                        Error('Wait timed out after '.concat(timeout, ' ms'))
                    );
                } else {
                    setTimeout(pollCondition, interval);
                }
            }, reject);
        };
        pollCondition();
    });
};
 
module.exports = exports;