I have an API calling function that I would like to return the response.json() content as well as the response.status together in a single object.
Like so:
Use async/await
. That will make things much cleaner:
async function getData(endpoint) {
const res = await fetch(endpoint, {
method: 'GET'
})
const body = await res.json()
return {
status: res.status,
body
}
}
You also might want to add a try / catch
block and a res.ok
check to handle any request errors or non 20x responses.