varresponse=awaitfetch("http://localhost:3000/failure",{method: "GET",}).then(resp=>{// first handle the statusif(!resp.ok){throwError(resp.statusText);}returnresp;}).then(resp=>{// then handle the json responsereturnresp.json()});console.log("this won't fire bc we didn't catch");
catching that unsuccessful fetch v1
try{varresponse=awaitfetch("http://localhost:3000/failure",{method: "GET",}).then(resp=>{// first handle the statusif(!resp.ok){throwError(resp.statusText);}returnresp;}).then(resp=>{// then handle the json responsereturnresp.json()});console.log("this won't fire");}catch(e){console.log(e.message);}console.log("this fires because we caught");
catching that unsuccessful fetch v2
varresponse=awaitfetch("http://localhost:3000/failure",{method: "GET",}).then(resp=>{// first handle the statusif(!resp.ok){throwError(resp.statusText);}returnresp;}).then(resp=>{// then handle the json responsereturnresp.json()console.log("this won't fire");}).catch(error=>{// also catch any other errorsconsole.log(error);});console.log("this fires because we caught");