Let's suppose we have
function first(){
console.log(1);
}
function second(){
console.log(2);
}
first();
second();
That will print
1
2
Now we put a timer inside the first() function.
function first(){
// Simulate a code delay
setTimeout( function(){
console.log(1);
}, 500 );
}
function second(){
console.log(2);
}
first();
second();
Now this will give the opposite.
2
1
That's because Javascript is an event-driven language. My question is how to define a callback function in the above example, so as to wait and print
1
2
Thanks, Theo.