Fork me on GitHub

Hypnotic Let JavaScript Sleep()!

by Lu Wang


We all knew that there is no sleep() in JavaScript.  

Although you can waste some time with a loop, the JavaScript interpreter is actually frozen such that you cannot receive any events, and that's why this is called busy-waiting. The following reaction game won't work with a busy-waiting sleep().

On the other hand, Hyponotic puts the JavaScript interpreter into sleep, deep deep sleep!

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!

Yes, we do have native sleep() with fibers, Rhino, or even XPCOM, but they rely on special extensions for interpreters, which are not general solutions.

On the other hand, Hypnotic is written in pure JavaScript. Did you know that you can run print(sleep.toSource());?

Oh Great! You have even more great ideas.

Would you leave some comments below and let us know?

Thank you!

But those days are finally over!

Hypnotic lets you write synchronous sleep() without freezing your browser.

Try it below and enjoy! :)


JavaScript is a single-threaded event-driven language. It is a kind of feature with which we rarely need to worry about locking, re-entrancy, atomic operations or other thread safety issues.

But an immediate side effect is that in JavaScript there is no sleep(), which exists in most other languages. In JavaScript we can never suspend the current thread as there is no extra thread to wake us up!

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!

Play around with the source code and hit Execute!

Only Firefox is supported right now. why?
Hypnotic uses some features in the future versions of JavaScript, which are only supported by Firefox currently.


 

Try your own code

Looks interesting?

Try your own code with sleep().

What is it good for?

  • Write synchronous code in JavaScript.
  • Of course asynchronous code still works.
  • Hypnotic can be easily extended.

Did Hypnotic impress you? Hopefully yes.


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.

  • Try something in the browser console.
  • Check the source code of this page.
Not helping?

  • Run console.log(sleep.toSource()); in the browser console.
  • And also do that in the source code area in the previous tab.
Even more confusing?

Read the technical details or this spoiler.

Hypnotic is an async JavaScript interpreter, which is evolved from Narcissus and streamline.js.


Did you have fun? Don't forget to add a star and leave some comments!



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:

Hypnotic introduces sleep() in JavaScript for those who really need it, but it does not encourage any unnecessary usage of it — that is why it is called Hypnotic!

And there is also a longer version.




comments powered by Disqus