tsbase
Version:
Base class libraries for TypeScript
36 lines • 1.26 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.Until = void 0;
var tslib_1 = require("tslib");
var tenSeconds = 10000;
/**
* Wait until the condition is met or the given limit is reached (whichever is first).
* @param condition
* @param interval
* @param limit
*/
function Until(condition, interval, limit) {
var _this = this;
if (interval === void 0) { interval = 0; }
if (limit === void 0) { limit = tenSeconds; }
return new Promise(function (resolve) {
var conditionMet = false;
var elapsedTime = 0;
var enabled = function () { return !conditionMet && elapsedTime < limit; };
var executer = setInterval(function () { return tslib_1.__awaiter(_this, void 0, void 0, function () {
return tslib_1.__generator(this, function (_a) {
elapsedTime += (interval || 1);
if (enabled()) {
conditionMet = condition();
}
else {
clearInterval(executer);
resolve(conditionMet);
}
return [2 /*return*/];
});
}); }, interval);
});
}
exports.Until = Until;
//# sourceMappingURL=Until.js.map