Coming from a Java backend which is more formal language with strong syntaxes and no function passing, I have some beginner queries on JavaScript execution.
var mongodb = require('mongodb');
var mongoClient = mongodb.MongoClient;
var dbUrl = 'mongodb://localhost:27017/test';
var con;
function callback(err, db) {
if (err) console.log('Unable to connect to the mongoDB server. Error:', err);
else {
console.log('Connection established to', dbUrl);
con = db;
findEmps(con, function() {
console.log("After find");
con.close();
});
}
}
mongoClient.connect(dbUrl, callback);
function findEmps(db, callback) {
var cursor = db.collection('emp').find();
//iterate on the result
cursor.each(function(err, result) {
assert.equal(err, null);
if (result != null) {
console.dir(result);
} else { //end of cursor where result is null
console.log("In ELSE");
callback(err, con);
}
});
}
console.log("END");
Why is END being printed first?