function asleep(ms) {
    return new Promise(resolve => setTimeout(resolve, ms))
}
const retry = async (fn, maxTry = 10) => {
    if (typeof fn !== 'function') throw new Error('fn is not a function')
    let count = 0
    const ts = new Date().getTime()
    // eslint-disable-next-line no-constant-condition
    while (true) {
        try {
            return await fn()
        } catch (e) {
            if (++count > maxTry) {
                console.log(`[failed after ${count - 1} retry]`)
                throw e
            }
            await asleep(1500)
            console.log(`[retry ${count} after ${new Date().getTime() - ts}ms]`)
        }
    }
}
export { asleep, retry }
