JavaScript Multiple Callback Function

Rob

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);
        });
    });
}
Frambot

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 

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How to callback one JavaScript function into multiple functions?

From Dev

Javascript array with callback function

From Dev

Usage of Javascript callback function

From Dev

Javascript Function with Callback and Parameters

From Dev

javascript callback on function

From Dev

JavaScript: custom callBack function

From Dev

Javascript with callback function not working

From Dev

OOP with Javascript and callback function

From Dev

Javascript callback function not work

From Dev

Creating a callback on javascript function

From Dev

Javascript anonymous callback function

From Dev

javascript callback function selection

From Dev

Javascript Callback function malfunction

From Dev

Callback with arrow function in javascript

From Dev

Javascript Custom Callback Function

From Dev

Bind callback function with multiple arguments

From Dev

Multiple Variables Javascript map() callback

From Dev

javascript callback function on a separate thread

From Dev

javascript callback function in chrome extension

From Dev

Javascript callback function for confirm bootbox

From Dev

callback function in javascript replace() is not called

From Dev

object callback function is undefined Javascript

From Dev

Javascript function callback dependant on timeout

From Dev

javascript callback function in chrome extension

From Dev

callback function argument in javascript / jQuery

From Dev

JavaScript: breaking out of a callback function

From Dev

Javascript function callback variable scope

From Dev

Javascript callback function in ajax request

From Dev

Javascript callback function firing twice