Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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