mirror of
https://github.com/meteor/meteor.git
synced 2026-05-02 03:01:46 -04:00
50 lines
1.3 KiB
JavaScript
50 lines
1.3 KiB
JavaScript
function isPromise(obj) {
|
|
return obj && typeof obj.then === 'function';
|
|
}
|
|
|
|
waitUntil = function _waitUntil(checkFunction, { timeout = 15_000, interval = 200, leading = true, description = '' } = {}) {
|
|
let waitTime = interval;
|
|
return new Promise((resolve, reject) => {
|
|
if (leading && checkFunction()) {
|
|
resolve();
|
|
return;
|
|
}
|
|
const handler = setInterval(() => {
|
|
const shouldWait = checkFunction();
|
|
if (isPromise(shouldWait)) {
|
|
shouldWait
|
|
.then(_shouldWait => {
|
|
if (_shouldWait) {
|
|
resolve();
|
|
clearInterval(handler);
|
|
return;
|
|
}
|
|
|
|
if (waitTime > timeout) {
|
|
console.error(description, 'timed out');
|
|
reject();
|
|
clearInterval(handler);
|
|
}
|
|
|
|
waitTime += interval;
|
|
})
|
|
.catch((_error) => {
|
|
console.error(description, _error?.message);
|
|
reject();
|
|
clearInterval(handler);
|
|
});
|
|
} else if (shouldWait) {
|
|
resolve();
|
|
clearInterval(handler);
|
|
} else {
|
|
if (waitTime > timeout) {
|
|
console.error(description, 'timed out');
|
|
reject();
|
|
clearInterval(handler);
|
|
}
|
|
waitTime += interval;
|
|
}
|
|
}, interval);
|
|
});
|
|
};
|