Node JS Async Function Call Problem

I've attached a text file with a code snippet to keep this simple.  Issue is I have two function that gather data from different sources and I want to append a main array with the values from the functions.  Problem is they run async so not all values are being added, only some from each function call. I need them to run in series (or get the callback working as needed) so that all of the values from each function are appended to the main array.  Thanks in advance...
Async-Function-Call.txt
kevbob650Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

RobOwner (Aidellio)Commented:
There will be a more elegant solution, however this will do the job:

getFirstData(dividedArray[i].toString(), function(res) {
            console.log("getting aqe data");
            for ( var a=0; a<arrMain.length; a++ ) {
                for ( var key in res[0]) {
                    if(key == arrMain[a][1]) {
                        arrMain[a].push(res[0][key]['TeamName']);
                        arrMain[a].push(res[0][key]['TeamID']);
                    }
                }
            }     						                                   
        
        getSecondData(dividedArray[i].toString(), function(res) {
            for(var k=0; k<arrMain.length; k++) {
                for(var j=0; j<response.length; j++) { 
                    if(response[j]['keyValue'] == arrMain[k][1]) {  // find like tickets, pull needed data elements and push to CBUS data array from above
                        arrMain[k].push(response[j]['Group']);
                        arrMain[k].push(response[j]['Organizatioin']);
                        arrMain[k].push(response[j]['Location']);								  
                    }
                }
            }
        });
});

Open in new window

0
RobOwner (Aidellio)Commented:
Where is your getFirstData function defined?  You can force synchronous if you need to but I can't direct you without knowing what it's calling
0
RobOwner (Aidellio)Commented:
This is where you would build a state machine (http://en.wikipedia.org/wiki/Finite-state_machine)

This means that your program should go through a series of "states" before completing, much like a flowchart.

here's an initial shot at it:

var flagfirst = false;
var flagsecond = false;

for(var i = 0; i < dividedArray.length; i++) {

	getFirstData(dividedArray[i].toString(), function(res) {
		console.log("getting aqe data");
		for ( var a=0; a<arrMain.length; a++ ) {
			for ( var key in res[0]) {
				if(key == arrMain[a][1]) {
					arrMain[a].push(res[0][key]['TeamName']);
					arrMain[a].push(res[0][key]['TeamID']);
				}
			}
		}
		flagfirst = true;
		checkStatus();
	});						                                   

	getSecondData(dividedArray[i].toString(), function(res) {
		for(var k=0; k<arrMain.length; k++) {
			for(var j=0; j<response.length; j++) { 
				if(response[j]['keyValue'] == arrMain[k][1]) {  // find like tickets, pull needed data elements and push to CBUS data array from above
					arrMain[k].push(response[j]['Group']);
					arrMain[k].push(response[j]['Organizatioin']);
					arrMain[k].push(response[j]['Location']);								  
				}
			}
		}
		flagsecond = true;
		checkStatus();
	});


}

function updatedb() {
	var mysqlConn = mysqlConnect(); // single connection to mysql
	populateMySql(arrMain,mysqlConn); // call the mysql routine to populate the database
}

function checkStatus() {
	// if both flags have been set then update the db
	if (flagfirst && flagsecond) {
		updatedb();
	}
}

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Kyle HamiltonData ScientistCommented:
This is where you should be using promises. You do not want the "pyramid of doom", ie. long sequences of callbacks. First of all, you loose the advantage of asynch, second your code will become entangled and hard to maintain.

the Q library for node makes promises easy:
https://github.com/kriskowal/q

specifically, look at Q.all
0
kevbob650Author Commented:
That got me where I was going.  I should have shared the functions I was calling but it got too complex.  I had to pass another variable in one of the callbacks, and use it instead of the "divided array".  Funny stuff async!  My code actually looks more sequential now, but it's using variables from the callback. So all is good.  I'm done with Node for a while. Off to Perl land now.  thanks again.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
JavaScript

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.