【问题标题】:Is this any good practice to nest try catch in redux-saga?这是在 redux-saga 中嵌套 try catch 的任何好习惯吗?
【发布时间】:2017-12-26 17:54:17
【问题描述】:

这就是我所拥有的……但我怀疑这是否合法。 基本上我想要实现的是我正在获取一个项目,如果它失败了,我想发出一个帖子请求。

    try {
        const response = yield call(axios.get, `/api/cars/${action.payload.car}`)
        yield put({type: RECEIVE_CAR, requestPending : false, data: response.data})
    } catch (e) {
    console.log(e)
        try {
            const response = yield call(axios.post, `/api/cars/`, action.payload)
            yield put({type: RECEIVE_CAR, requestPending : false, data: response.data})
        } catch (error) {
            console.log(error)
        }
    }

【问题讨论】:

标签: api reactjs redux redux-saga


【解决方案1】:

这是 Javascript 开发中的常规做法。建议在 Javascript 参考 here

您还可以为失败编写另一个 saga,然后在 catch 语句中调用该 saga。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-06-05
    • 1970-01-01
    • 2015-08-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-06-27
    • 1970-01-01
    相关资源
    最近更新 更多