17

I have been trying to figure out the Callback function feature in Javascript for a while without any success. I probably have the code messed up, however I am not getting any Javascript errors, so I supposed the syntax is somewhat correct.

Basically, I am looking for the getDistanceWithLatLong() function to end before the updateDB() begins, and then make sure that ends before the printList() function begins.

I have it working with a hardcoded "setTimeout" call on the functions, but I am overcompensating and forcing users to wait longer without a need if the Callback stuff would work.

Any suggestions? Below is the code:

function runSearchInOrder(callback) {
    getDistanceWithLatLong(function() {
        updateDB(function() {
            printList(callback);
        });
    });
}
3
  • 1
    Ignoring the problem about multiple callbacks, what are you trying to accomplish? Commented May 20, 2014 at 0:39
  • I need the following 3 functions to run and finish in order: getDistanceWithLatLong(); updateDB(); printList(): Just listing them that way I would have thought would work, but when i print things in the console.log sometimes the printList() runs before the loops of the other function is done. Commented May 20, 2014 at 0:40
  • Are the getDistance... and updatedDB functions expecting a callback? I do not see why it wouldn't work if they do. Commented May 20, 2014 at 1:17

4 Answers 4

27

To accomplish this, you need to pass the next callback into each function.

function printList(callback) {
  // do your printList work
  console.log('printList is done');
  callback();
}

function updateDB(callback) {
  // do your updateDB work
  console.log('updateDB is done');
  callback()
}

function getDistanceWithLatLong(callback) {
  // do your getDistanceWithLatLong work
  console.log('getDistanceWithLatLong is done');
  callback();
}

function runSearchInOrder(callback) {
    getDistanceWithLatLong(function() {
        updateDB(function() {
            printList(callback);
        });
    });
}

runSearchInOrder(function(){console.log('finished')});

This code outputs:

getDistanceWithLatLong is done
updateDB is done
printList is done
finished 
Sign up to request clarification or add additional context in comments.

1 Comment

what about what passing variables from one callback to another?
1

wouldn't this work:

function callback(f1, f2) {
    f1();
    f2();
}

As for passing arguments, be creative.

Comments

1

function1 = (callback1, callback2, callback3) => {
  setTimeout(() => {
    console.log("function 1 timed out!");
    callback1(callback2, callback3);
  }, 1500);
}

function2 = (callback1, callback2) => {
  setTimeout(() => {
    console.log("function 2 timed out!");
    callback1(callback2);
  }, 1500);
}

function3 = (callback1) => {
  setTimeout(() => {
    console.log("function 3 timed out!")
    callback1()
  }, 1500);
}

function4 = () => {
  setTimeout(() => {
    console.log("function 4 timed out!")
  }, 1500);
}

function1(function2, function3, function4);

Just pass down the callbacks from the first function and execute each one, passing down the rest.

OUTPUT

function 1 timed out!
function 2 timed out!
function 3 timed out!
function 4 timed out!

Comments

0

In JavaScript, everything is an object, including functions. That is why you are able to pass callbacks as parameters - you are passing a function as if it were any other object.

In each function declaration, you need to run the callback.

function runSearchInOrder(callback) {

    ...

    callback();
}

function getDistanceWithLatLong(callback) {

    ...

    callback();
}

function updateDB(callback) {

    ...

    callback();
}

Then your code posted above should work.

Comments

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.