How to add 5 seconds delay without locking up UI using ES6 async/await
WebDev
Posted on July 24, 2022
You will need to promisify sleep function manually.
function timeout(ms) {
return new Promise(resolve => setTimeout(resolve, ms));
}
async function sleep(fn, ...args) {
await timeout(3000);
return fn(...args);
}
Btw, to slow down your loop you probably don't want to use a sleep function that takes a callback and defers it like this:
while (goOn) {
// other code
var [parents] = await Promise.all([
fn(...args).then(
//code here
),
timeout(5000)
]);
// other code
}
💖 💪 🙅 🚩
WebDev
Posted on July 24, 2022
Join Our Newsletter. No Spam, Only the good stuff.
Sign up to receive the latest update from our blog.
Related
webdev Understanding HTTP, Cookies, Email Protocols, and DNS: A Guide to Key Internet Technologies
November 30, 2024