【发布时间】:2020-07-20 19:50:02
【问题描述】:
我在哪里发出 API 请求:
function* search(value){
// return new Promise(async (resolve, reject)=>{
// try {
// const res = await axios.get(`https://www.breakingbadapi.com/api/characters?name=${value}`)
// console.log(res.data)
// resolve(res.data)
// } catch (error) {
// reject(error)
// }
// })
return axios.get(`https://www.breakingbadapi.com/api/characters?name=${value}`)
.then(res=>{
console.log(res.data)
})
.catch(err=>{
// console.log(err)
})
}
我在哪里调用这个函数来获取结果并将其置于状态:
function* startSearch(value){
try {
yield put({type:'loading'})
const person = yield call(search, value)
console.log("SAGA",person)
yield put({type:'success', payload:person})
} catch (error) {
yield put({type:'failure'})
}
}
如您所见,我已尝试将 api 调用封装在 promise 和典型的 .then .catch 中。无论我保持控制台记录一个承诺,并且没有一种对象按预期存储在状态中。根据文档,如果生成器返回一个承诺,则 yield 调用应该暂停生成器,但这似乎没有发生。
编辑:这就是为什么你需要远离屏幕的人。我需要做的就是从搜索功能中删除“*”。超级简单。
【问题讨论】:
标签: javascript reactjs redux redux-saga