Skip to content

Instantly share code, notes, and snippets.

@kieranbarker
Last active Mar 21, 2022
Embed
What would you like to do?
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