Reputation: 8413
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!
Upvotes: 1
Views: 195
Reputation: 11245
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);
});
Upvotes: 2