js promise json code example
Example 1: get promise result from json() javascript
fetch(myRequest)
.then(response => response.json())
.then(data => {
console.log(data);
});
myFunction().then(data => {
console.log(data);
});
Example 2: fetch response json or text
const _fetch = async props => {
const { endpoint, options } = props
return await fetch(endpoint, options)
.then(async res => {
if (res.ok) {
return await res.clone().json().catch(() => res.text())
}
return false
})
.catch(err => {
console.error("api", "_fetch", "err", err)
return false
})
}