Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Only await cache#get if it's a promise #82

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion benchmark.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import dns from 'node:dns';
import Benchmark from 'benchmark';
import CacheableLookup from './source/index.js';
import CacheableLookup from './source/index.mjs';

const cacheable = new CacheableLookup();
const suite = new Benchmark.Suite();
Expand Down
12 changes: 11 additions & 1 deletion source/index.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,10 @@ const ignoreNoResultErrors = dnsPromise => {
});
};

const isProm = value => {
return Boolean(value && typeof value.then === 'function');
}

const ttl = {ttl: true};
const all = {all: true};
const all4 = {all: true, family: 4};
Expand Down Expand Up @@ -215,7 +219,13 @@ export default class CacheableLookup {

async query(hostname) {
let source = 'cache';
let cached = await this._cache.get(hostname);
let maybeProm = this._cache.get(hostname);

if( isProm( maybeProm ) ) {
maybeProm = await maybeProm;
}

let cached = maybeProm;

if (cached) {
this.stats.cache++;
Expand Down