Proper way to catch exception from JSON.parse

后端 未结 5 1963
轻奢々
轻奢々 2020-12-02 07:13

I’m using JSON.parse on a response that sometimes contains a 404 response. In the cases where it returns 404, is there a way to catch an exception and then exec

5条回答
  •  清歌不尽
    2020-12-02 08:02

    You can try this:

    Promise.resolve(JSON.parse(response)).then(json => {
        response = json ;
    }).catch(err => {
        response = response
    });
    

提交回复
热议问题