From 995cd6ed73d9200b1e7a3b2356089517b55ecfab Mon Sep 17 00:00:00 2001 From: Stefan Aebischer Date: Wed, 15 Jan 2020 14:40:10 -0500 Subject: [PATCH] Avoid conccurent _syncTime calls --- lib/request.js | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) diff --git a/lib/request.js b/lib/request.js index 7cfbfe3..7ad6527 100644 --- a/lib/request.js +++ b/lib/request.js @@ -15,6 +15,7 @@ module.exports = class Request { constructor(key, secret, livenet=false) { this.baseUrl = baseUrls[livenet === true ? 'livenet' : 'testnet']; this._timeOffset = null; + this._syncTimePromise = null; if(key) assert(secret, 'Secret is required for private enpoints'); @@ -105,6 +106,18 @@ module.exports = class Request { } async _syncTime() { - this._timeOffset = await this.getTimeOffset(); + if(this._syncTimePromise !== null) return this._syncTimePromise; + + this._syncTimePromise = new Promise(async (resolve, reject) => { + try { + this._timeOffset = await this.getTimeOffset(); + this._syncTimePromise = null; + resolve(); + } catch(err) { + reject(err); + } + }); + + return this._syncTimePromise; } }