0

I am trying to figure out how to create asynchronous functions for a web app. I am doing a database query, manipulating the data to a format that is more convenient, and then trying to set my router to pass back that file.

//Module 1
//Module 1 has 2 functions, both are necessary to properly format
function fnA(param1){
    db.cypherQuery(query, function(err, result){
        if(err){
            return err;
        }
        var reformattedData = {};
        //code that begins storing re-formatted data in reformattedData

        //the function that handles the rest of the formatting
        fnB(param1, param2);
    });
});

function fnB(param1, reformattedData){
    db.cypherQuery(query, function(err, result){
        if(err){
            return err;
        }
        //the rest of the reformatting that uses bits from the second query 
        return reformattedData;
    });
});

exports.fnA = fnA;

Then in my router file:

var db = require('module1');

router.get('/url', function(req,res,next){
    db.fnA(param1, function(err, result){
        if (err){
            return next(err);
        }
        res.send(result);
    });
});

When I tried to test this out by hitting the URL indicated by the router, it just loads indefinitely.

I know what I have above is wrong, since I never wrote my function to require a callback. When I tried figuring out how to rewrite it though, I got really confused - How do I write my function to have a callback when the asynchronous stuff happens inside it?

Can someone help me rewrite my functions to use callbacks correctly, so that when I actually use the function, I can still work with the asynchronous response?

1 Answer 1

1

You use db.fa from your router file, and pass the second parameter as a callback function. but the function signature don't have the cb param and doesnt use it.

The main idea - you try to initiate an async operation and cannot know when it ends, so you send it a callback function to get triggered when all operations are done.

Fixed code should be like that:

//Module 1
//Module 1 has 2 functions, both are necessary to properly format
function fnA(param1, cb1){
    db.cypherQuery(query, function(err, result){
        if(err){
            cb1(err); <-- return error to original call
        }
        var reformattedData = {};
        //code that begins storing re-formatted data in reformattedData

        //the function that handles the rest of the formatting
        fnB(param1, param2, cb1);
    });
});

function fnB(param1, reformattedData, cb1){
    db.cypherQuery(query, function(err, result){
        if(err){
            cb1(err); <-- return error to original call
        }
        //the rest of the reformatting that uses bits from the second query 
        cb1(false, dataObjectToSendBack); <--- This will call the anonymouse function in your router call
    });
});

exports.fnA = fnA;

Router file:

var db = require('module1');

router.get('/url', function(req,res,next){
    db.fnA(param1, function(err, result){ <-- This anonymous function get triggered last 
        if (err){
            return next(err);
        }
        res.send(result);
    });
});
Sign up to request clarification or add additional context in comments.

5 Comments

the return err is not correct because returning it where you are will do nothing. You need a way to tell the callback that there's an error because right now, you're not communicating anything to the outside world when there's an error.
@jfriend00 you are right, that was a copy paste of Dude's code. fixed
Ok, this is making much more sense now. Thanks a lot Ben! Just to make sure that I understand this now, does this mean that cb1 is not defined until it is called in the router file? By the code that says: ...function(err, result){ if(err)...
@Dude yep, its an anonymous function
@Ben, you are awesome! This core concept finally snapped into place for me, and makes perfect sense now. Man, I love this place.

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.