Reputation: 355
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
Upvotes: 4
Views: 2264
Reputation: 10391
response.json() is async and returns promise
change this
const res = response.json()
to
const res = yield response.json()
webpackbin example
Upvotes: 2