From 19b5d6f926dc28e771c858df4145df946806671e Mon Sep 17 00:00:00 2001 From: Dragon Fire Date: Thu, 11 Feb 2021 14:06:29 -0500 Subject: [PATCH] Prevent race conditions in Pokemon game data fetching --- structures/pokemon/Pokemon.js | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/structures/pokemon/Pokemon.js b/structures/pokemon/Pokemon.js index 8a4d795f..c43b0b33 100644 --- a/structures/pokemon/Pokemon.js +++ b/structures/pokemon/Pokemon.js @@ -1,6 +1,6 @@ const request = require('node-superfetch'); const path = require('path'); -const { removeDuplicates, firstUpperCase } = require('../../util/Util'); +const { removeDuplicates, firstUpperCase, delay } = require('../../util/Util'); const missingno = require('../../assets/json/missingno'); const versions = require('../../assets/json/pokedex-location'); @@ -58,6 +58,7 @@ module.exports = class Pokemon { this.moveSet = data.missingno ? data.moveSet : []; this.moveSetVersion = data.missingno ? data.moveSetVersion : null; this.gameDataCached = data.missingno || false; + this.gameDataFetching = data.missingno || false; this.missingno = data.missingno || false; this.cry = data.id > store.pokemonCountWithCry ? null @@ -169,11 +170,17 @@ module.exports = class Pokemon { async fetchGameData() { if (this.gameDataCached) return this; + if (this.gameDataFetching) { + await delay(1000); + return this.fetchGameData(); + } + this.gameDataFetching = true; await this.fetchDefaultVariety(); await this.fetchMoves(this.rawMoveSet); await this.fetchHeldItemNames(); await this.fetchOtherVarieties(); await this.fetchChain(); + this.gameDataFetching = false; this.gameDataCached = true; return this; }