Skip to content

Commit

Permalink
Fix the awaitable collect() calls.
Browse files Browse the repository at this point in the history
In case if non-native promises are used, the Promise object won't be instanceof Promise. That's still valid in JS, as Promise is something that implements then(). It's always safe to cast a maybe-promise into native Promise with Promise.resolve().
  • Loading branch information
farcaller committed Aug 8, 2024
1 parent c1d76c5 commit 58ed7d1
Show file tree
Hide file tree
Showing 4 changed files with 4 additions and 8 deletions.
3 changes: 1 addition & 2 deletions lib/counter.js
Original file line number Diff line number Diff line change
Expand Up @@ -108,8 +108,7 @@ class Counter extends Metric {

async get() {
if (this.collect) {
const v = this.collect();
if (v instanceof Promise) await v;
const v = await Promise.resolve(this.collect());
}

return {
Expand Down
3 changes: 1 addition & 2 deletions lib/gauge.js
Original file line number Diff line number Diff line change
Expand Up @@ -107,8 +107,7 @@ class Gauge extends Metric {

async get() {
if (this.collect) {
const v = this.collect();
if (v instanceof Promise) await v;
const v = await Promise.resolve(this.collect());
}
return {
help: this.help,
Expand Down
3 changes: 1 addition & 2 deletions lib/histogram.js
Original file line number Diff line number Diff line change
Expand Up @@ -107,8 +107,7 @@ class Histogram extends Metric {

async getForPromString() {
if (this.collect) {
const v = this.collect();
if (v instanceof Promise) await v;
const v = await Promise.resolve(this.collect());
}
const data = Object.values(this.hashMap);
const values = data
Expand Down
3 changes: 1 addition & 2 deletions lib/summary.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,7 @@ class Summary extends Metric {

async get() {
if (this.collect) {
const v = this.collect();
if (v instanceof Promise) await v;
const v = await Promise.resolve(this.collect());
}
const hashKeys = Object.keys(this.hashMap);
const values = [];
Expand Down

0 comments on commit 58ed7d1

Please sign in to comment.