Nice Article!. IMO await with promise chaining is not ideal and should not be used at all. By default await statement will wait for the execution of the promise, so adding a then block is not required.
constres=awaitfetch('/users').then(res=>{if(res.status<200||res.status>=400){thrownewError('Server responded with status code '+res.status);}returnres;}).then(res=>res.json());
The above statement can be simplified with a single await within a try/catch block like following
try{constres=awaitfetch('/users');if(res.status<200||res.status>=400){thrownewError('Server responded with status code '+res.status);}constdata=res.json();// do something with data}catch(err){// handle error}
The problem is that you need to do await res.json() as well, but otherwise your approach works. I think the await fetch().then(res => res.json()) pattern is common enough that it's worth mentioning.
For further actions, you may consider blocking this person and/or reporting abuse
We're a place where coders share, stay up-to-date and grow their careers.
Nice Article!. IMO
awaitwith promise chaining is not ideal and should not be used at all. By defaultawaitstatement will wait for the execution of the promise, so adding athenblock is not required.The above statement can be simplified with a single
awaitwithin atry/catchblock like followingThe problem is that you need to do
await res.json()as well, but otherwise your approach works. I think theawait fetch().then(res => res.json())pattern is common enough that it's worth mentioning.