-1

I have a function inside of which there is a nested callback function structure. I want that first "mother" function to return a certain value, after it's been calculated in the callback functions sequence. However. it doesn't really work. Here's a simplified version of the code, do you think you could help?

console.log(finalResult());

function finalResult() {    

var finalanswer = firstFunction(secondFunction);

function firstFunction (callback) {
    var notion = 1;
    callback(null, notion);
}

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

return finalanswer;  

}  

Thank you!

**UPDATE - THE ORIGINAL CODE**

 return getContexts(makeQuery);


 function getContexts (callback) {

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

        if(err) {
            err.type = 'neo4j';
            return callback(err);
        }
        // No error? Pass the contexts to makeQuery function
        return callback(null,cypherAnswer);



    });




}

function makeQuery (err,answer) {
    // Error? Display it.
    if (err) console.log(err);

    // Define where we store the new contexts
    var newcontexts = [];

    // This is an array to check if there are any contexts that were not in DB
    var check = [];

    // Go through all the contexts we received from DB and create the newcontexts variable from them
    for (var i=0;i<answer.data.length;i++) {
        newcontexts.push({
            uid: answer.data[i].uid,
            name: answer.data[i].name
        });
        check.push(answer.data[i].name);
    }

    // Now let's check if there are any contexts that were not in the DB, we add them with a unique ID
    contexts.forEach(function(element){
        if (check.indexOf(element) < 0) {
            newcontexts.push({
                uid: uuid.v1(),
                name: element
            });
        }
    });

    return newcontexts;

}
Aerodynamika
  • 7,883
  • 16
  • 78
  • 137

1 Answers1

0

You're setting finalanswer equal to the return of firstFunction however, if you look at the body of firstFunction, it does not return anything. The last line of firstFunction should be:

return callback(null, notion);

I quickly tested this in the Chrome console and it seems to work as expected and logs 2 to the console.

UPDATE

Now that the original code has been posted I would update the code as such:

// call getContexts with a callback when complete
getContexts(function(err, contexts){
    console.log(err);
    console.log(contexts);
});

function getContexts (callback) {
    dbneo.cypherQuery(context_query, function(err, cypherAnswer){
        if(err) {
            err.type = 'neo4j';
            return callback(err);
        }
        // No error? Pass the contexts to makeQuery function
        var contexts = makeQuery(null,cypherAnswer);

        // we have our answer, call the callback
        callback(null, contexts);
    });
}

function makeQuery (err,answer) {
    // Error? Display it.
    if (err) console.log(err);

    // Define where we store the new contexts
    var newcontexts = [];

    // This is an array to check if there are any contexts that were not in DB
    var check = [];

    // Go through all the contexts we received from DB and create the newcontexts variable from them
    for (var i=0;i<answer.data.length;i++) {
        newcontexts.push({
            uid: answer.data[i].uid,
            name: answer.data[i].name
        });
        check.push(answer.data[i].name);
    }

    // Now let's check if there are any contexts that were not in the DB, we add them with a unique ID
    contexts.forEach(function(element){
        if (check.indexOf(element) < 0) {
            newcontexts.push({
                uid: uuid.v1(),
                name: element
            });
        }
    });

    return newcontexts;
}
Andrew
  • 1,279
  • 2
  • 13
  • 19
  • This will work as long as the code does not make any async calls. The code in the original question does not make async calls so you are correct, but @deemeetree should be aware that this will NOT work as soon as the code makes an async call. – Hector Correa May 27 '14 at 19:07
  • Strange, it doesn't work for me... – Aerodynamika May 27 '14 at 19:22
  • 1
    The code in the question does not work for you or the code which you're not showing us does not work for you? Based on the code you posted, my answer is correct. If you want help with the code that is not working, I suggest you post that. – Andrew May 27 '14 at 19:29
  • @Andrew the original code is above - i added it. looks like my code is not working because the return of the second function is nested inside a database query function and probably its return is not the return of the function... but i don't know how to solve it... – Aerodynamika May 27 '14 at 22:55
  • Check my updated answer. As others already pointed out, you called a function asynchronously and tried to return a value from within that function. the better approach here is to pass an anonymous function into getContexts that will get called when the asynchronous function is finished. Then you can log your answer as shown. – Andrew May 28 '14 at 02:40