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();
}
2
  • 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 Commented Dec 22, 2015 at 3:22
  • Possible duplicate of How do I return the response from an asynchronous call? Commented Dec 22, 2015 at 3:24

2 Answers 2

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();
}
Sign up to request clarification or add additional context in comments.

Comments

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>

6 Comments

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);
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
but even if you use a timeout there is no way to clearly predict when c will finish its execution
@DOfficial what kind of operation is happening in c?
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.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.