Skip to content

Instantly share code, notes, and snippets.

@mjackson
Created February 23, 2016 22:23
Show Gist options
  • Star 8 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mjackson/e1996e4d6205e313ea0b to your computer and use it in GitHub Desktop.
Save mjackson/e1996e4d6205e313ea0b to your computer and use it in GitHub Desktop.
fetch JSON with a callback
function fetchJSON(url, options, callback) {
if (typeof options === 'function') {
callback = options
options = {}
}
options = options || {}
const headers = (options.headers || (options.headers = {}))
headers.Accept = 'application/json'
fetch(url, options)
.then(response => response.json())
.then(json => callback(null, json), callback)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment