Get the JSON data from a Response object.
This file contains 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
/** | |
* 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