Javascript has been this synchronous language since forever, and it took me a while to shift my mind to understand the recent async/await concepts.
Recently I needed to make a simple sleep within a function, but there’s no builtin for that. How the hell can I make a simple sleep without blocking the main thread??
Here’s what I ended up finding on StackOverflow, using a simple promise and a timeout to resolve it:
function sleep(ms) {
return new Promise(resolve => setTimeout(resolve, ms));
}
Usage:
await sleep(5000);