I have a mongoose connect function in which I try to wait for reconnecting if the first attempt fails:
async connect() {
const options = {...}
try {
console.log("starting")
await this._connectWithRetry(options)
console.log("finished")
} catch (err) {
winston.error(`Could not connect to Mongo with error: ${err}`)
}
}
private async _connectWithRetry(options) {
return new Promise( async (resolve, reject) => {
try {
winston.info("Connecting to mongo...")
await mongoose.connect(this.dbURI, options)
winston.info("Connection successful.")
resolve()
} catch (err) {
winston.info("Failed to connect to mongo. Retrying in 5 seconds...")
setTimeout( async () => {
await this._connectWithRetry(options)
}, 5000)
}
})
}
It successfully waits until I'm connected. But once I connect, the second console line is not hit ("finished"). so I figure that my promise resolution is buggy. What am I doing wrong?