Last active
March 8, 2022 03:04
-
-
Save darioriverat/fddf9be57d7a7f61ce5c50c34c4ec56d to your computer and use it in GitHub Desktop.
Some class to get user Config from API in a browser
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Config { | |
endpoint = 'https://somewebsite.com/config'; | |
constructor(userId) { | |
this.userId = userId; | |
} | |
async getValueFor(key) { | |
if (this.data) { | |
// getting from cache | |
return this.data[key]; | |
} | |
return await fetch(`${this.endpoint}/${this.userId}`) | |
.then((response) => response.json()) | |
.then((data) => { | |
this.data = data; | |
return this.data[key]; | |
}); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment