so I'm having real trouble with this - been stuck on it for weeks.
My first function queries a MySQL database and returns a response, which then gets processed in my second function.
The issue is that JS is single-threaded and I can't figure out how to get it to run in the order I want:
- Function 1 is fired.
- The function fetches the DB response and is then processed.
- The reponse is handed to function 2, which then does more processing.
However at the moment, the program jumps straight to function two without allowing time for the query, and the function ends up returning 'undefined'.
var test = functionOne (functionTwo);
console.log(test); //returning 'undefined'
function functionOne(callback) {
//QUERY TAKES A LONG TIME
client.query("[QUERY]", function(err, result) {
callback(null, result);
});
}
function functionTwo(err, result) {
//More processing - slightly slow
return result;
}
EDIT:
Here is the full file.
var client = require('./dbHelper');
var convert = require('./genGIF');
var exportPPM = require('./makePPM');
var fs = require('fs');
module.exports = function() {
var test = queryDatabase (handleResult);
console.log("print: " + test);
}
function queryDatabase(callback) {
//Query our database
client.query("SHOW TABLES FROM mathsDB", function(err, result) {
if (err) {
callback(err);
}
else {
//Calculate the length of our table and then the last image in the table
var currentImage = result[result.length - 1]["Tables_in_mathsDB"];
currentImage = currentImage.substring(5);
callback(null, currentImage);
console.log(currentImage);
}
});
}
function handleResult(err, currentImage) {
fs.stat("./img/image" + currentImage + ".gif", function(err, stat) {
if (err==null) {
var imageFile = "/img/image" + currentImage + ".gif";
return imageFile;
}
else {
//Check if we have a .PPM file made for the image instead, then generate a .GIF
fs.stat("./img/image" + currentImage + ".ppm", function(err, stat) {
if (err==null) {
convert.convert("image" + currentImage);
var imageFile = "/img/image" + currentImage + ".gif";
return imageFile;
}
else {
//Generate the .GIF if no .GIF or .PPM already.
exportPPM.make();
convert.convert("image" + currentImage);
var imageFile = "/img/image" + currentImage + ".gif";
return imageFile;
}
});
}
});
}