Last active
March 21, 2022 09:42
-
-
Save kieranbarker/50c49f7c4966c4b88d7e21f1657fa924 to your computer and use it in GitHub Desktop.
Get the JSON data from a Response object.
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
/** | |
* 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