Hi there, very interesting topic, thanks for this library!
You can use a combination of setTimeout, AbortController and the Fetch API to achieve a similar result, while still leveraging the power of promises.
constcreateSignalWithTimeout=(seconds)=>{constmilliseconds=seconds*1000;constabortController=newAbortController();constsignal=abortController.signal;lettimeout=null;timeout=window.setTimeout(abortController.abort,milliseconds);constcancelSignal=()=>{window.clearTimeout(timeout);};return{cancelSignal,signal};};const{signal,cancelSignal}=createSignalWithTimeout(1);fetch("https://jsonplaceholder.typicode.com/users",{signal}).then((response)=>{if(response.ok){cancelSignal();returnresponse.json();}thrownewError("Bad response from the server");}).then((users)=>{console.log(users);}).catch(({message})=>{console.error(message);});
This is a very great use-case for using this obscure AbortController. Although you don't really manage what error message is thrown, you can still detect the instanceof the error which will be AbortError in that case. See MDN.
I'm a web developer, graphic designer, type designer, musician, comicbook-geek, LEGO-collector, food lover … as well as husband and father, located just south of Copenhagen, Denmark.
I'm a web developer, graphic designer, type designer, musician, comicbook-geek, LEGO-collector, food lover … as well as husband and father, located just south of Copenhagen, Denmark.
Hi there, very interesting topic, thanks for this library!
You can use a combination of setTimeout, AbortController and the Fetch API to achieve a similar result, while still leveraging the power of promises.
This is a very great use-case for using this obscure AbortController. Although you don't really manage what error message is thrown, you can still detect the
instanceof
the error which will beAbortError
in that case. See MDN.It's exactly what I'm using, if you look at the code - it just adds the AbortError to the same errorHandler.
The result is a promise ;-)
You are correct, I didn't look at your code, very well done then!
I'll let this code snippet for people wanting an alternative without the start/stop callback or the error list.
👍