I implement callback function in node js. but I have doubt in callback function.I tried two function in node js one callback function and another normal function.both function i tried to run its given same result.I do no any one explain my code.
callback_function.js
const MongoClient = require('mongodb').MongoClient;
var ObjectId = require('mongodb').ObjectID
// Connection URL
var db =" "
MongoClient.connect('mongodb://localhost:27017', (err, client) => {
// Client returned
db = client.db('olc_prod_db');
gener(function(id)
{
db.collection('Ecommerce').find({ _id: new ObjectId(id) }, function(err,result)
{
console.log("hello")
})
})
function gener(callback)
{
db.collection('Ecommerce').find({}).toArray(function(err,result)
{
console.log("hai")
})
callback("5ccac2fd247af0218cfca5dd")
}
});
normal_function.js
const MongoClient = require('mongodb').MongoClient;
var ObjectId = require('mongodb').ObjectID
// Connection URL
var db =" "
MongoClient.connect('mongodb://localhost:27017', (err, client) => {
// Client returned
db = client.db('olc_prod_db');
gener()
function data()
{
console.log("hello")
}
function gener()
{
db.collection('Ecommerce').find({}).toArray(function(err,result)
{
console.log("hai")
})
data()
}
});
it showing both result hello and hai