I am using the express framework to handle requests. Some of the steps involve invoking asynchronous functions. Eventually I would like the return the value puted in one of these functions.
app.post("/hello", function(req, res) {
...
...
myFunc(finalFantasy);
}
function myFunc(callback) {
...
callback();
}
function finalFantasy() {
//I want to return this value in the response
var result = "magic";
}
How can I use the value calculated in finalFantasy function in the response?
I am using the express framework to handle requests. Some of the steps involve invoking asynchronous functions. Eventually I would like the return the value puted in one of these functions.
app.post("/hello", function(req, res) {
...
...
myFunc(finalFantasy);
}
function myFunc(callback) {
...
callback();
}
function finalFantasy() {
//I want to return this value in the response
var result = "magic";
}
How can I use the value calculated in finalFantasy function in the response?
Share Improve this question asked Dec 18, 2013 at 17:35 dopplesoldnerdopplesoldner 9,49912 gold badges46 silver badges57 bronze badges 1-
With the code structure identical to shown in the question, just make
myFunc
return the result ofcallback
- and use it. – raina77ow Commented Dec 18, 2013 at 17:38
4 Answers
Reset to default 3You have to pass the res
instance to the callback, or use callback like the second example.
app.post("/hello", function(req, res) {
...
...
myFunc(finalFantasy, req, res);
}
function myFunc(callback, req, res) {
...
callback(req, res);
}
function finalFantasy(req, res) {
//I want to return this value in the response
var result = "magic";
res.end(result);
}
Another example is this:
app.post("/hello", function(req, res) {
...
...
var i = 3;
myFunc(i, function(data) {
res.end(data); // send 4 to the browser
});
}
function myFunc(data, callback) {
data++; //increase data with 1, so 3 bee 4 and call the callback with the new value
callback(data);
}
app.post("/hello", function(req, res) {
...
...
var result = myFunc(finalFantasy);
}
function myFunc(callback) {
...
return callback();
}
function finalFantasy() {
//I want to return this value in the response
var result = "magic";
return result;
}
you almost had it, just like @raina77ow said in the ment, pass the result back up
Keep passing res
along, and in your final "response generating function", send data to it. The general way to do this with express is to use the middleware cascade, storing persistent values in res.locals:
app.get("/", function(req, res, next) {
// do things
next();
},function(req, res, next) {
// do more things
next();
},function(req, res, next) {
// do even more things!
next();
},function(req,res) {
// and finally we form a response
res.write(things);
});
Although you normally put this in a middleware file and then include that, calling its functions:
// middleware "lib/functions.js" file
module.exports = {
a: function(req, res, next) { ... },
b: function(req, res, next) { ... },
c: function(req, res, next) { ... }
}
and then in your app.js, main.js, or similarly obvious main file:
var mylib = require("./lib/functions");
...
app.get("/", mylib.a, mylib.b, mylib.c);
...
done. Nice and organised, and automatic passthrough of streams through the next
concept.
// wrap finalFantasy using an anonymous function
app.post("/hello", function(req, res) {
...
...
myFunc(function () {
var ret = finalFantasy();
res.send(ret);
});
}
function myFunc(callback) {
...
callback();
}
function finalFantasy() {
//I want to return this value in the response
var result = "magic";
}