This question is about how to handle promise. Example:
fetch(url)
.then(res => res.json() )
.then(data => console.log(data) )
.catch(err => console.log("ERROR:", err.message))
My purpose is to inspect the res before pass it to json(), like:
fetch(url)
.then(res => {
console.log(res) // this does not contain response body
console.log(res.text()) //<-- how to do this
res.json()
})
As far as I know, text() also returns a promise, like json(). In case I also want the raw text, how to handling multiple promise at the same time?
One option might be just use text() like this:
fetch(url)
.then(res => { res.text() })
.then(raw => {
let data = JSON.parse(raw) //this is no longer a promise...
console.log(data)
})
.catch(err => console.log("ERROR:", err.message))
Is this a valid option, or "idiomatic" way of handling such situation? However even this is possible, I am still interested if it is possible to execute multiple promises and handle them gracefully.