0

I need to know how to make a function wait for a callback from another function before executing. Below is a simplified scenario of what I am trying to accomplish.

function a(){
  b();
  // Wait for function c to callback
  var callback = function();
  // then continue function a() logic...
}
function b(){
  c();
  return false;
}

function c(){
  //trigger callback in function a
    callback();
}
DOfficial
  • 485
  • 6
  • 20
  • In the perfect world yes but in short no. Function a is called in other places and only needs to listen for the callback in a specified instance where it needs to wait for data returned from c to proceed – DOfficial Dec 22 '15 at 03:22
  • Possible duplicate of [How do I return the response from an asynchronous call?](http://stackoverflow.com/questions/14220321/how-do-i-return-the-response-from-an-asynchronous-call) – Leon Adler Dec 22 '15 at 03:24

2 Answers2

1

Put the rest of a's logic into the callback, and if you need, use a reference to a's this

function a(){
  b();
  var that = this;
  var callback = function(){
    that.variables
    // then continue function a() logic...
  }
}
function b(){
  c();
  return false;
}

function c(){
  //trigger callback in function a
    callback();
}
Joseph Young
  • 2,758
  • 12
  • 23
1

What you need to do is to pass a callback to b and c as shown below and call the callback in c after its execution... assuming c is doing an async operation.

function a() {
  snippet.log('inside a');
  b(function() {
    //code that should be executed after `c` must be here... note that you cannot return a value from here to the caller of `a`
    snippet.log('inside callback');
  });
  snippet.log('after b')
}

function b(callback) {
  snippet.log('inside b');
  c(callback);
  return false;
}

function c(callback) {
  snippet.log('inside c');
  setTimeout(function() {
    snippet.log('inside async timeout');
    //trigger callback in function a
    callback();
  }, 100);
}

a();
<!-- Provides the `snippet` object, see http://meta.stackexchange.com/a/242144/134069 -->
<script src="http://tjcrowder.github.io/simple-snippets-console/snippet.js"></script>
Arun P Johny
  • 384,651
  • 66
  • 527
  • 531
  • Ok this may work. What if the callback never gets fired though? Should I call a timeout function to trigger the callback? ie setTimout(functioin(){callback();},15000); – DOfficial Dec 22 '15 at 03:40
  • I get that. What I mean is that if something happens in function C where the callback doesn't get triggered it is more important that function a continues it's execution. Therefore should I set a timeout to call the callback no matter what after a set Timout? I think im answering my own question – DOfficial Dec 22 '15 at 03:44
  • but even if you use a timeout there is no way to clearly predict when c will finish its execution – Arun P Johny Dec 22 '15 at 03:46
  • @DOfficial what kind of operation is happening in `c`? – Arun P Johny Dec 22 '15 at 03:46
  • function a is a form submission function. b() is an address form validation function that calls c() for an ajax request to a third party address validation software. I need the form submit() to wait for the response from c before continuing to process the data. – DOfficial Dec 22 '15 at 03:49
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/98628/discussion-between-arun-p-johny-and-dofficial). – Arun P Johny Dec 22 '15 at 03:52