I have 2 separate functions, both are making a GET request. After completing, I need to add up number from response1 with number from response2. So basically I want make 3rd function, that will add up results from previous 2 functions. The problem is that 3rd function executes before 1st and 2nd.
I tried callbacks, but seems it's not working as expected. Below you can find a simple example and I want to understand the basics before implementing it in my code. Example with callback I tried:
function first(callback){
setTimeout(function(){
console.log(1);
}, 500);
callback()
}
function second(){
console.log(2);
}
function third(){
first(second);
}
third();
Example without callback:
function first(){
setTimeout(function(){
console.log(1);
}, 500);
}
function second(){
console.log(2);
}
function third(){
first();
second();
}
third();
https://jsfiddle.net/u8a592pz/
Currently this function executes as:
2
1
What I want to get:
1
2