4

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

Vickyexpert
  • 3,147
  • 5
  • 21
  • 34
mtangula
  • 355
  • 1
  • 4
  • 13

1 Answers1

2

response.json() is async and returns promise

change this

const res = response.json()

to

const res = yield response.json()

webpackbin example

Kokovin Vladislav
  • 10,241
  • 5
  • 38
  • 36