Skip to content

Commit 58ed7d1

Browse files
committed
Fix the awaitable collect() calls.
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().
1 parent c1d76c5 commit 58ed7d1

File tree

4 files changed

+4
-8
lines changed

4 files changed

+4
-8
lines changed

lib/counter.js

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -108,8 +108,7 @@ class Counter extends Metric {
108108

109109
async get() {
110110
if (this.collect) {
111-
const v = this.collect();
112-
if (v instanceof Promise) await v;
111+
const v = await Promise.resolve(this.collect());
113112
}
114113

115114
return {

lib/gauge.js

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -107,8 +107,7 @@ class Gauge extends Metric {
107107

108108
async get() {
109109
if (this.collect) {
110-
const v = this.collect();
111-
if (v instanceof Promise) await v;
110+
const v = await Promise.resolve(this.collect());
112111
}
113112
return {
114113
help: this.help,

lib/histogram.js

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -107,8 +107,7 @@ class Histogram extends Metric {
107107

108108
async getForPromString() {
109109
if (this.collect) {
110-
const v = this.collect();
111-
if (v instanceof Promise) await v;
110+
const v = await Promise.resolve(this.collect());
112111
}
113112
const data = Object.values(this.hashMap);
114113
const values = data

lib/summary.js

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,8 +50,7 @@ class Summary extends Metric {
5050

5151
async get() {
5252
if (this.collect) {
53-
const v = this.collect();
54-
if (v instanceof Promise) await v;
53+
const v = await Promise.resolve(this.collect());
5554
}
5655
const hashKeys = Object.keys(this.hashMap);
5756
const values = [];

0 commit comments

Comments
 (0)