Skip to content

Instantly share code, notes, and snippets.

@adeleke5140
Created April 6, 2023 23:17
Show Gist options
  • Select an option

  • Save adeleke5140/9dc05c03bcc8ccf6719c8ca7676935f4 to your computer and use it in GitHub Desktop.

Select an option

Save adeleke5140/9dc05c03bcc8ccf6719c8ca7676935f4 to your computer and use it in GitHub Desktop.
cache
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