easy-polling
Version:
Easy-polling aims to achieve one simple and pure task:\ Check result of an asynchronous task until the result matched required criteria.\ This process is so called "polling" or "poll"!.
24 lines (21 loc) • 485 B
JavaScript
module.exports = async function (
fnAsyncTask,
fnValidate,
msInterval,
msTimeout
) {
let result = await fnAsyncTask();
let totalTime = 0;
while (!fnValidate(result)) {
if (totalTime > msTimeout) return null;
await wait(msInterval);
result = await fnAsyncTask();
totalTime += msInterval;
}
return result;
};
const wait = function (msInterval) {
return new Promise((resolve) => {
setTimeout(resolve, msInterval);
});
};