Created
April 6, 2023 23:17
-
-
Save adeleke5140/9dc05c03bcc8ccf6719c8ca7676935f4 to your computer and use it in GitHub Desktop.
cache
This file contains hidden or 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
| const _cache: {[url: string]:string} = {} | |
| async function fetchWithCache(url:string){ | |
| if(url in cache){ | |
| return _cache[url] | |
| } | |
| const response = await fetch(url) | |
| const text = response.text() | |
| _cache[url] = text | |
| return text | |
| } | |
| let requestState: 'loading' | 'success' | 'error' | |
| async function getUser(userId: string){ | |
| requestStatus = 'loading' | |
| const profile = await fetchWithCache(`/user/${userId}`) | |
| requestStatus = 'success' | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment