Created
February 23, 2016 22:23
-
-
Save mjackson/e1996e4d6205e313ea0b to your computer and use it in GitHub Desktop.
fetch JSON with a callback
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
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