0

Here is an over view of my js file where I am calling 2 function foo1 and foo2.

function foo1(){
 // do some ajax call which takes random amount of time
}

function foo2(){
 // do something which needs foo1() being already  done
}

foo1();
// reach here only if foo1 is complete
foo2();

ps. I dont want to use jQuery or knockout. Also have already looked into How to wait for a func...

Community
  • 1
  • 1
Waterfr Villa
  • 1,217
  • 1
  • 10
  • 33
  • 1
    you don't have to do anything. function calls in JS block. it's not multi-threaded. unless foo1() is doing an ajax call or something, foo2() will **NOT** get called until foo1() returns. – Marc B Sep 16 '15 at 17:02
  • @MarcB Thank you I added ajax call being part of foo1. Appreciated your comment – Waterfr Villa Sep 16 '15 at 17:07
  • 1
    then you have the `success:` handler in the ajax stuff call `foo2()` when the ajax results get returned. – Marc B Sep 16 '15 at 17:07

1 Answers1

0

Given you say foo1() is doing ajax, you'd need something like this:

function foo1() { 
   $.ajax( ....
       success: function() { foo2(); }
   );
}
function foo2() {
    console.log("foo1() finished");
}

foo1();

So when the ajax call returns (successfully), it'll automatically call foo2(), and the only thing you need to do is call foo1().

Marc B
  • 356,200
  • 43
  • 426
  • 500