How to handle errors in fetch() responses with Redux-Saga?

后端 未结 3 1022
感动是毒
感动是毒 2020-12-15 04:46

I try to handle Unauthorized error from server using redux-saga. This is my saga:

function* logIn(action) {
  try {
    const user = yield call(         


        
3条回答
  •  长情又很酷
    2020-12-15 05:41

    if you want to have that if statement verifying the response status if(res.status >= 200 && res.status < 300) { you need to have it inside your first promise where res is defined, it's currently inside the resolved promise for res.json()

    .then(res => {
       if (res.status >= 200 && res.status < 300) {
          res.json().then(json => {
             return json
        }
      })
    })
    

提交回复
热议问题