add module for waiting

It isn't recommended tho
This commit is contained in:
buzz-lightsnack-2007 2024-05-15 09:19:07 +08:00
parent 78fd39ca65
commit fe2c59920d

18
scripts/utils/wait.js Normal file
View file

@ -0,0 +1,18 @@
/*
Wait until a condition is met. It isn't recommended since you're better off using the callback on the function itself, but it's still here just in case.
@param {Function} watch the condition to watch for
@param {Object} options the options
@return {Promise} the callback
*/
const wait = (watch, options) => {
return new Promise((resolve, reject) => {
(watch)
? resolve()
: setTimeout(() => {
wait(watch, options).then(resolve).catch(reject);
}, (((options && (typeof options).includes(`obj`)) ? options[`time`] : false) ? Math.abs(parseInt(options[`time`])) : 100));
});
}
export default wait;