mtangula mtangula - 3 months ago 23
Javascript Question

Redux-Saga not able to correctly read response from fetch api

Hi How do I read the data coming back from a fetch call:

export function* fetchMessages(channel) {
yield put(requestMessages())
const channel_name = channel.payload
try {
const response = yield call(fetch,'/api/messages/'+channel_name)

const res = response.json()
console.log(res)
yield put(receiveMessages(res,channel))


} catch (error){
yield put(rejectMessages(error))
}


}

When i console.log(res) I get:

Promise {[[PromiseStatus]]: "pending", [[PromiseValue]]: undefined}
__proto__
:
Promise
[[PromiseStatus]]
:
"resolved"
[[PromiseValue]]
:
Array[7]


How do I get my "info" (side Array[7] from this promise? I am new to all this. Thanks

Answer

response.json() is async and returns promise

change this

const res = response.json()

to

const res = yield response.json()

webpackbin example