I need to manage a queue of DB calls that will be executed only once the connection is established. The DB object is created and stored as a member of the module upon connection.
DB Module:
var db = {
localDb: null,
connectLocal: (dbName) => {
// Do stuff
this.localDb = new PouchDB(dbName) // has an allDocs() method
}
}
Adding calls to the queue:
var dbQueue = []
function getDocs () {
dbQueue.push (
db.localDb.allDocs () // allDocs() not yet defined; returns a promise
)
}
// Will be called once connected and the queue is not empty:
function processQueue () {
Promise.all (dbQueue)
.then(...)
}
If getDocs() is invoked before db.connectLocal() sets db.localDb, then an error similar to the following may occur because db.localDb is not yet defined:
TypeError: Cannot read property 'then' of undefined
Is it possible to add an undefined method that returns a promise to an array for later resolution using Promise.all()? Any other suggestions on how I can tackle this problem?
Additionally, I am utilizing Vue.js and PouchDB in this scenario.