4 ms·
The only original insight of this blog post is that it's nice to be able to use `sleep()` without async/await in Go, which is also true for Rust and Rust has As
by sebstefan 2y ago
The only original insight of this blog post is that it's nice to be able to use `sleep()` without async/await in Go, which is also true for Rust and Rust has Async.
It does not stem from async/await that Javascript doesn't have sleep()
- iudqnolq 2y agofunction sleep(ms) { return new Promise((resolve) => setTimeout(() => resolve(), ms) ); }
- CryZe 2y agoI still don't know why that isn't just provided for you.
- sebstefan 2y agoYou're returning a promise, that's a red function. It's async https://journal.stuffwithstuff.com/2015/02/01/what-color-is-your-function/ https://journal.stuffwithstuff.com/2015/02/01/what-color-is-... Sleep() should halt, this returns immediately If I want my code to actually halt, I can either make myself red and use await on your red function, or I resign myself to put everything after the sleep in a .then()
- iudqnolq 2y agoI misread your comment. It wouldn't make sense to have a non-async sleep in a browser, as it is an event-loop based primarily single-threaded JavaScript runtime.
- deleted 2y ago[deleted]
- jitl 2y agoI just found out you can sync sleep in JavaScript. I’m using it to implement multi-process sync file locks that need to interoperate with a large non-async framework (eslint). Atomics.wait(this.sleepBuffer, 0, 0, ms)