mtangula mtangula - 1 year ago 73
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()
yield put(receiveMessages(res,channel))

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


When i console.log(res) I get:

Promise {[[PromiseStatus]]: "pending", [[PromiseValue]]: undefined}

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

Answer Source

response.json() is async and returns promise

change this

const res = response.json()


const res = yield response.json()

webpackbin example