Understanding callbacks
Callbacks exploit JavaScript’s capability to pass functions. There are two essential parts to this technique:
- A function that is passed as an argument to another function
- The passed function is executed when a certain event happens
Let’s create a basic example to illustrate this concept. In the following code snippets, we will show how the callback is defined as an argument and how a function is passed as an argument when the execution occurs:
- In this example, we will define a function (
doSomething
) that expects a function as an argument:const doSomething = (cb) => { console.log('Doing something...'); cb(); };
- At this point, we have a function called
doSomething
that receives a function as an argument and executes it as the last step, this illustrates the idea that the callbacks are just a pattern where we expect that the next function to the executed is actually called as the final...