IOC is your friend

"Don't call us, we'll call you"

Inversion of Control (IoC) is a design principle that suggests that the control flow of a program should be inverted: instead of the programmer calling the library, the library should call the programmers function.

A simple example is the addEventListener function in JavaScript. We register a callback function for a specific event, and the browser calls our function when the event occurs:

// we invert the control so that the browser calls our function
document.addEventListener("click", () => {
  console.log("clicked");
});