Skip to content

Instantly share code, notes, and snippets.

@kissmygritts
Created July 28, 2022 16:19
Show Gist options
  • Save kissmygritts/8781324509fa480db87cea06042bc19b to your computer and use it in GitHub Desktop.
Save kissmygritts/8781324509fa480db87cea06042bc19b to your computer and use it in GitHub Desktop.
async function testConnection (db) {
try {
const c = await db.connect()
c.done()
console.log('CONNECTED')
} catch (error) {
console.log('RETRYING CONNECTION')
await testConnection(db)
}
}
const wait = delay => {
return new Promise(resolve => {
setTimeout(() => { resolve() }, delay)
})
}
function waitForDb (db) {
return new Promise(async (resolve, reject) => {
let count = 0
let dbReady = false
while (dbReady && count <= 6) {
count++
try {
const c = await db.connect()
c.done()
dbReady = true
console.log('CONNECTED TO DATABASE')
} catch (error) {
console.log('DATABASE ISNT READY, RETRYING')
}
await wait(count * count * 1000)
}
if (count > 6) reject()
resolve()
})
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment