I'm using node js and I have three layers in my code Controller, Service, DAO. Execution flow is from controller to DAO via Service and again control comes back from DAO to Controller. There are call back functions in DAO layer which are setting outputs to variables. But problem is call back functions are executing after control coming back to controller. Im ending up by getting a undefined as output for my variables. This is because Node is an Async.
Is there a way to change this flow,so that first call back function completes execution and then control comes back from DAO to service and to controller.
You have to pass the parts of your code, which should be executed after your DAO stuff finished as a callback to your DAO layer. This often looks a little bit ugly, but it's the node.js (asynchronus) way, sometimes called the "callback hell". If you need more information please provide some code examples of what you're trying to do.
As a starting place, you might want to try using the async library: https://github.com/caolan/async or "npm install async". It sounds like what you may want is async.series(), which will allow you to structure certain sets of callbacks to run sequentially, allowing you more control over the flow of your program. Good tutorial here: http://www.sebastianseilund.com/nodejs-async-in-practice.