I'm just a beginner in JavaScript, and I would like to wait for the end of an action before executing the next one. I've already searched an answer to my problem on Google, but all I've found are the using of callbacks to string functions together. OK, but that is not what I need. I don't want to make a chain, I want to make an imbrication, like this :
function f1() { function f2() { function f3() { return result; } return result; } return result; } So that, when I call console.log(f1), it prints the results which is calculated in f3(). Is there a simple way to do this ?
Thank you.
Sara.
EDIT: Well, I think I have to give a better example ! In concrete terms, I work on a web server with express and a a database with mongoose and mongodb. I have four types of path : path concerning jobs, groups, workers and users. When I get a page, I have to get the right list of elements in the database to display : if I go on the page /job, I get the list of existing jobs, if I go on the page /user, I get the list of existing users, etc. In the routing file, I call another file which will manage the connection to the database. Here are my codes :
In the file route.js :
var mongo = require('./mongo'); exports.list = function(req, res) { var data = mongo.read(req.params.type);//the type of element we are looking for (jobs, user...) res.render('liste', { table: data; //In the view, we get table.name, table.status, for each element of the table } } In my mongo.js :
exports.read = function(type) { var result; start(); //It's the function which start the database, create the schemas, etc... if(type == 'job')//4 conditions, in which we get the right model (in the var model) according to the page we're looking for model.find(function(err, data) { if(err) { ... } else { result = data; } } return result; //It return nothing, because it do this before doing the model.find(...) } I hope it is clearer.
EDIT (again...) : Thank you everybody for your answers and thank you Sagi Isha for giving me the solution :)