/
githubmirror
/
meteor
Обзор
Документация
Войти
/
githubmirror
/
meteor
Код
Запросы
0
Пакеты
0
Релизы
0
Аналитика
Безопасность
devel
packages/test-helpers/wait.js
50 строк
1 KB
Nacho Codoñer
fix waitUntil scenario
04 фев 2025, 19:51
04 фев 2025, 19:51
bc9e24e
Код
Авторство
О чём код?
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) => { const shouldWait = checkFunction(); if (leading && !isPromise(shouldWait) && shouldWait) { 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); }); };