初始化

This commit is contained in:
yziiy
2025-08-11 11:06:07 +08:00
parent 083bc37c00
commit 5607d11395
19772 changed files with 3108723 additions and 18 deletions

View File

@@ -0,0 +1,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;