Node.js and Express: How to return response after asynchronous operation Node.js and Express: How to return response after asynchronous operation express express

Node.js and Express: How to return response after asynchronous operation


To access a variable in a different function, when there isn't a shared scope, pass it as an argument.

You could just pass res and then access both query and send on the one variable within the function.


For the purposes of separation of concerns, you might be better off passing a callback instead.

Then do_search only needs to know about performing a query and then running a function. That makes it more generic (and thus reusable).

searcher.do_search(res.query, function (data) {    res.send(...);});function do_search(query, callback) {    callback(...);}