Skip to content

Instantly share code, notes, and snippets.

@kieranbarker
Last active March 21, 2022 09:42
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 kieranbarker/50c49f7c4966c4b88d7e21f1657fa924 to your computer and use it in GitHub Desktop.
Save kieranbarker/50c49f7c4966c4b88d7e21f1657fa924 to your computer and use it in GitHub Desktop.
Get the JSON data from a Response object.
/**
* Get the JSON data from a Response object.
* @param {Response} response The Response object.
* @returns {Promise} The JSON data or an Error.
*/
async function getJSON(response) {
if (response.ok) {
const data = await response.json();
return Promise.resolve(data);
}
const { status, statusText } = response;
const error = new Error(`${status} ${statusText}`);
return Promise.reject(error);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment