diff --git a/lib/index.js b/lib/index.js index c7b579d..93fd314 100644 --- a/lib/index.js +++ b/lib/index.js @@ -364,9 +364,11 @@ export default class Now extends EventEmitter { }); } - getNameservers (domain, { fallback = false } = {}) { - try { - return this.retry(async (bail, attempt) => { + getNameservers (domain) { + return new Promise((resolve, reject) => { + let fallback = false; + + this.retry(async (bail, attempt) => { if (this._debug) console.time(`> [debug] #${attempt} GET /whois-ns${fallback ? '-fallback' : ''}`); const res = await this._fetch(`/whois-ns${fallback ? '-fallback' : ''}?domain=${encodeURIComponent(domain)}`); if (this._debug) console.timeEnd(`> [debug] #${attempt} GET /whois-ns${fallback ? '-fallback' : ''}`); @@ -375,18 +377,17 @@ export default class Now extends EventEmitter { if ((!body.nameservers || body.nameservers.length === 0) && !fallback) { // if the nameservers are `null` it's likely // that our whois service failed to parse it - return this.getNameservers(domain, { fallback: true }); + fallback = true; + throw new Error('Invalid whois response'); } return body; } else { + if (attempt > 1) fallback = true; throw new Error(`Whois error (${res.status}): ${body.error.message}`); } - }); - } catch (err) { - if (fallback) throw err; - return this.getNameservers(domain, { fallback: true }); - } + }).then((body) => resolve(body)); + }); } // _ensures_ the domain is setup (idempotent)