sequencing function calls in javascript - are callbacks the only way?

Posted by tim on Stack Overflow See other posts from Stack Overflow or by tim
Published on 2012-09-05T03:28:08Z Indexed on 2012/09/05 3:38 UTC
Read the original article Hit count: 84

I read through various threads like this one for example.

But it really escapes me how to accomplish the following:

I have 4 functions, and want them happen one after another in sequence. Notice they are in incorrect order, to get my point across. I want the result that will output "1, 2, 3, 4'

function firstFunction(){
  // some very time consuming asynchronous code...
  console.log('1');
}
function thirdFunction(){
  // definitely dont wanna do this until secondFunction is finished
  console.log('3');
}
function secondFunction(){
  // waits for firstFunction to be completed
  console.log('2');
}
function fourthFunction(){
  // last function, not executed until the other 3 are done.
  console.log('4');
}

I tried to figure out callbacks but am getting lost :(

Isn't there some simple way to do this? Like looping through an array...

© Stack Overflow or respective owner

Related posts about JavaScript

Related posts about callback