You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
asyncfunctiongetQuote(){constresponse=awaitfetch('/quote?by=Mark+Twain');constquote=awaitresponse.json();returnquote;}// {// "quote": "If you tell the truth, you don't have to remember anything.",// "source": "notebook",// "date": "1894"// }
asyncfunctioncheckedFetch(input: RequestInfo,init?: RequestInit){constresponse=awaitfetch(input,init);if(!response.ok){// An exception becomes a rejected Promise in an async function.thrownewError(`Request failed: ${response.status}`);}returnresponse;}
constcheckedFetch: typeoffetch=async(input,init)=>{// ~~~~~~~~~~~~// 'Promise<Response | HTTPError>' is not assignable to 'Promise<Response>'// Type 'Response | HTTPError' is not assignable to type 'Response'constresponse=awaitfetch(input,init);if(!response.ok){returnnewError('Request failed: '+response.status);}returnresponse;}
asyncfunctionfetchANumber(
...args: Parameters<typeoffetch>): Promise<number>{constresponse=awaitcheckedFetch(...args);constnum=Number(awaitresponse.text());if(isNaN(num)){thrownewError(`Response was not a number.`);}returnnum;}