Global event handling for the win!


Part 1 – Callback Hell

“You have power over your mind—not outside events. Realize this, and you will find strength.”

– Marcus Aurelius, Meditations

Please note: In this article series, I refer to JavaScript as a programming language, in particular its Event API.

JavaScript is a programming language that, in my opinion, is characterized by one concept in particular: asynchronous programming. This means that JavaScript algorithms have to react more often than other programming languages to events (user input, server responses, background calculations, to name a few) that start or end at an unspecified time.

For this reason, all functions that need to take asynchrony into account provide an additional object that is notified whenever an event of a certain type occurs. This object can have different names - listener, continuation function - or callback function.

The unspecified, parallel execution of various callback functions at unspecified times poses serious problems, especially for programmers with little experience - but also quite often for experienced programmers.

These problems are often summarized under the term "Callback Hell".

I don't want to go into detail about all these problems, but I want to present you a universally applicable pattern, with which I avoid these problems very successfully.

As the title of this series suggests, my solution is to consistently orchestrate, handle and process events from a central, global location: the document root. In conjunction with certain conventions, this approach gives me maximum control over behavior that is inherently difficult to control.

More about this in the second part of this series.

Files

index.zip Play in browser
Mar 16, 2022

Leave a comment

Log in with itch.io to leave a comment.