Probably worded poorly, I understand async/promises/callbacks.
What I'm trying to do is create a module which can be required (database.js
), which I can then call methods such as database.insert()
and database.read()
.
So here is my code:
require('dotenv').config()
const {MongoClient} = require('mongodb');
const client = new MongoClient(process.env.MONGO_URI);
var db, queue;
client.connect().then(e => {
console.log('connected to database');
db = client.db('filemanager');
queue = db.collection('queue');
});
function insert (data, cb) {
queue.insertOne(data)
.then(res => {cb()})
}
module.exports = {
insert: insert,
}
The question is: How do I make it so when database.insert()
is called, it will process instantly if db and queue are already defined, but if they aren't it will wait until they are, then will continue processing like normal?