Skip to content

Instantly share code, notes, and snippets.

@halkeand
Last active December 15, 2017 23:07
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save halkeand/1f2e915fc6198899cbac2fe78b42c768 to your computer and use it in GitHub Desktop.
Save halkeand/1f2e915fc6198899cbac2fe78b42c768 to your computer and use it in GitHub Desktop.
fetch-and-process
const render = (data) => console.log(data)
const apiUrl = 'https://jsonplaceholder.typicode.com/albums'
const fetchAsync = async (url) => {
let resp = await fetch(url)
let data = await resp.json()
return data
}
const fetchAndProcess = (url, process) => {
return fetchAsync(url)
.then(data => process(data))
.catch(e => process(e.message))
}
fetchAndProcess(apiUrl, render)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment