setTimeout() lets you delay the execution of some function in another context. Your current context has to exit. This is very annoying when you need to define logic about timing in your program. Consider how you can implement the following reaction game with setTimeout().
On the other hand, with sync sleep() powered by Hypnotic, life becomes much easier now!
Promise pattern saves us from nested brackets, it lets us write asynchronous code in an intuitive way. But still you need to define lots of callback functions, with extra efforts to maintain states. How about writing a loop with the promise pattern? Sounds easy?
On the other hand, Hypnotic brings the missing sleep() for you! You want delays in loops, branches and even with exception handling? Piece of cake!
You've been playing around with streamline.js, continuation.js or Wind.js, haven't you? These semi-auto CPS transformers give us brand new experience writing asynchronous functions in a synchronous fashion. They are almost the ultimate solution except for that we have to mark all the asynchronous functions in some way — this cannot be done by the transformers, you won't know if a function is synchronous or not until it is actually resolved in the runtime.
Try to rename or override sleep() in the below and see if Hypnotic still works!
On the other hand, Hypnotic is written in
Oh Great! You have even more great ideas.
Would you leave some comments below and let us know?
But those days are finally over!
Hypnotic lets you write synchronous sleep() without freezing your browser.
Try it below and enjoy! :)
Although busy-waiting and setTimeout() might help in some situations, it may be very annoying writing in the asynchronous fashion, especially when writing timing logics.
Finally those days are over! Kind of.
Play the reaction game and see how Hypnotic could make your life easier!
In order not to lose fun, it's highly recommended that you try to figure it out yourself.
Just think for a few minutes about how you can implement it.
No idea? Here are a few hints.
console.log(sleep.toSource());in the browser console.
Thank you so much for all your comments! It appears that previously I did not emphasize the purpose of this project, and sorry if you were confused. Allow me to explain:
And there is also a longer version.