1

I have an exports function in my Node.Js / Express app, which I want to return a certain value obtained through a series of callback functions inside of it. Trying to make it work for several hours but can't find the right way... Maybe you can help? Here's the code:

exports.finalResult = function() {

    var finalanswer = firstFunction(secondFunction);

    function firstFunction(callback) {
        dbneo.cypherQuery(context_query, function(err, cypherAnswer) {

            if (err) {
                err.type = 'neo4j';
                return callback(err);
            }
            var notion = 1;
            // Probably this return below doesn't work because it's inside of a db query function...
            return callback(null, notion);

        });

    }

    function secondFunction(err, notion) {
        if (err) console.log(err);
        var answer = notion + 1
        return answer;
    }

    return finalanswer;

}

and then I call from another file this exports function to obtain a result like

 console.log(options.finalResult());

but it returns undefined.

Please, help!

1 Answer 1

2
exports.finalResult = function(finalCallback) {

    var finalanswer = firstFunction(secondFunction);

    function firstFunction(callback) {
        dbneo.cypherQuery(context_query, function(err, cypherAnswer) {

            if (err) {
                err.type = 'neo4j';
                return callback(err);
            }
            var notion = 1;
            callback(null, notion);
        });
    }

    function secondFunction(err, notion) {
        if (err) console.log(err);
        var answer = notion + 1
        finalCallback(answer);
    }
}

// Usage
options.finalResult(function (result) {
    console.log(result);
});
Sign up to request clarification or add additional context in comments.

Comments

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.