0

So I'm having a lot of trouble getting my module to return the value I want it to. Basically it looks into my filesystem and returns the last created GIF file. If there is no GIF, it looks for a PPM (another file type) and generates a GIF and returns that. If there's no PPM, it'll look into the MySQL database and generate a GIF based on what's in there. All of this works fine.

This module is called with a HTTP request in Express, where currentImg() is calling the module:

//REQUEST CURRENT IMAGE
app.get('/img', function(req, res) {
    console.log(currentImg());
});

This request always returns 'undefined'. I've tried a bit of debugging and the module is returning 'undefined' before it finishes getting the return from the 'queryDatabase(returnFile)' line in the module.exports function.

// CurrentImg

module.exports = function() {
    imageFile = "";
    queryDatabase(returnFile);
    return imageFile;
}

function queryDatabase(callback) {
    imageFile = "";
    client.query("SHOW TABLES FROM mathsDB", function(err, result) {
            if (err)
                    console.log(err);
            else
                    currentImage = result[result.length - 1]["Tables_in_mathsDB"];
                    currentImage = currentImage.substring(5);
                    callback;
    });
}

function returnFile() {
    fs.stat("./img/image" + currentImage + ".gif", function(err, stat) {
            if (err==null) {
                    imageFile = "/img/image" + currentImage + ".gif";
                    return imageFile;
            }
            else {
                    fs.stat("./img/image" + currentImage + ".ppm", function(err, stat) {
                            if (err==null) {
                                    convert.convert("image" + currentImage);
                                    imageFile = "/img/image" + currentImage + ".gif";
                                    return imageFile;
                            }

                            else {
                                    exportPPM.make();
                                    setTimeout(waitConvert, 500);
                                    function waitConvert() {
                                            convert.convert("image" + currentImage);
                                            imageFile = "/img/image" + currentImage + ".gif";
                                            return imageFile;
                                    }
                            }
                    });
            }
    });
}

I've tried doing some reading into callbacks, as you can see in my code, however that didn't seem to solve anything. I've read a bit in to promises too but they don't seem to provide a solid solution either.

Can anyone provide me with a pointer in the right direction to get this running in a non-blocking way?

Thanks.

4
  • 2
    callback; doesn't actually do anything. You need to call it, like any other function. Commented Apr 14, 2016 at 23:03
  • blog.slaks.net/2015-01-04/async-method-patterns Commented Apr 14, 2016 at 23:04
  • Since returnFile() will need to wait queryDatabase to complete ..you may use async.waterfall to run function in series one after another. github.com/caolan/async#waterfall Commented Apr 14, 2016 at 23:13
  • Yeah my mistake, I am using callback(); Commented Apr 14, 2016 at 23:19

1 Answer 1

0

Use async waterfall to run function in series one after another. https://github.com/caolan/async#waterfall. So something like this.

async.waterfall([
function (callback) {
imageFile = "";
client.query("SHOW TABLES FROM mathsDB", function(err, result) {
        if (err)
                console.log(err);
        else
                currentImage = result[result.length - 1]["Tables_in_mathsDB"];
                currentImage = currentImage.substring(5);
                callback(currentImage)
});

},

    function(currentImage, callback) {
      // arg1 now equals 'one' and arg2 now equals 'two'
      fs.stat("./img/image" + currentImage + ".gif", function(err, stat) {
            if (err==null) {
                    imageFile = "/img/image" + currentImage + ".gif";
                    return imageFile;
            }
            else {
                    fs.stat("./img/image" + currentImage + ".ppm", function(err, stat) {
                            if (err==null) {
                                    convert.convert("image" + currentImage);
                                    imageFile = "/img/image" + currentImage + ".gif";
                                    return imageFile;
                            }

                            else {
                                    exportPPM.make();
                                    setTimeout(waitConvert, 500);
                                    function waitConvert() {
                                            convert.convert("image" + currentImage);
                                            imageFile = "/img/image" + currentImage + ".gif";
                                            return imageFile;
                                    }
                            }
                    })
            }
  callback(null,currentImage)  })


    }

], function (err, currentImage) {
    console.log(currentImage);
});
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks for your reply. I've had a read over the documentation for Async and while it seems like it should work, I feel that my issue should be solvable through the use of native JS. I don't believe in importing a new library for every little utility or function, however others maybe find this acceptable. Thanks!
Async utility will be hopefully in ES7. If you need to solve it through current ES6, go for Promise.all. Iterate both functions accordingly.

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.