What is Inversion of Control?

JavaScript

Inversion of Control

There's part of our program that we're in control of executing. There's another portion of our program that we're not in control of executing. The way we express that is to take the first half of our program that executes now and the second half of our program that executes in the callback, and when we give our callback to somebody else, that what's invert the control and it puts them in control of when and what manner to execute the second half of our program.

In the example given below line 1 and 2 represents the first half of our program, and line 3 and 4 represents the second half of our program. Here, we are passing our second half of program to setTimeout which is a built in JavaScript engine facility and we don't have trust issues with setTimeout. But, if we are passing our second half of program to some another third party utility we don't know how they will run our callback and we can run into some trouble.

// line 1
setTimeout(function () {
// line 3
// line 4
});
// line 2
00:00

Table of Contents