Redux-Saga无法正确读取fetch api的响应

时间:2016-08-29 08:07:39

标签: javascript redux es6-promise saga redux-saga

您好如何读取从提取呼叫返回的数据:

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))
   }      
}     

当我在console.log(res)时,我得到:

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

如何获取我的"信息" (这个承诺的侧面阵列[7]?我对这一切都不熟悉。谢谢

1 个答案:

答案 0 :(得分:2)

response.json()是异步并返回承诺

更改此

const res = response.json()

const res = yield response.json()

webpackbin示例