Interesting behavior is observed in this code, where 'output 2' is logged before 'output 1' due to the setTimeout
function.
const func1 = () => {
setTimeout(() => {
console.log('output 1');
}, 3000);
};
const func2 = () => {
console.log('output 2');
};
func1();
func2();
Despite using a callback in the second snippet, the execution order remains the same. The question arises: why does func2
execute before func1
? Is there a way to ensure that func2
runs after func1
?
const func1 = () => {
setTimeout(() => {
console.log('output 1');
}, 3000);
};
const func2 = () => {
console.log('output 2');
};
const main = (a, b) => {
a();
b();
};
main(func1, func2);