Tech Lead/Team Lead. Senior WebDev.
Intermediate Grade on Computer Systems-
High Grade on Web Application Development-
MBA (+Marketing+HHRR).
Studied a bit of law, economics and design
Location
Spain
Education
Higher Level Education Certificate on Web Application Development
Tech Lead/Team Lead. Senior WebDev.
Intermediate Grade on Computer Systems-
High Grade on Web Application Development-
MBA (+Marketing+HHRR).
Studied a bit of law, economics and design
Location
Spain
Education
Higher Level Education Certificate on Web Application Development
Hahaha no judgements. Still I let you some polling thingy I had here that may be more suitable:
constpoll=async({fn,validate,interval,maxAttempts})=>{letattempts=0;constexecutePoll=async(resolve,reject)=>{constresult=awaitfn();attempts++;if(validate(result)){returnresolve(result);}elseif(maxAttempts&&attempts===maxAttempts){returnreject(newError('Exceeded max attempts'));}else{setTimeout(executePoll,interval,resolve,reject);}};returnnewPromise(executePoll);};
poll function is a higher-order function that returns a function, executePoll.
executePoll function returns a promise and will run recursively until the condition is met.
Args explained:
fn: an API request (or another async thingy that suits for this polling).
validate: Test function to see if the data matches what we want, in which case it will end the poll.
interval: To specify how much it wait between poll requests.
maxAttempts: how many tries before throwing an error.
I couldn't sleep last night and read this comment at 3am. Coincidentally, today, I needed this poll. Works great in my usEffect().
The only thing that caught me is that the parameters to the above poll function are in a class, instead of individual parameters.
Thanks for the code!
Tech Lead/Team Lead. Senior WebDev.
Intermediate Grade on Computer Systems-
High Grade on Web Application Development-
MBA (+Marketing+HHRR).
Studied a bit of law, economics and design
Location
Spain
Education
Higher Level Education Certificate on Web Application Development
Didn't get the meaning of the sentence "The only thing that caught me is that the parameters to the above poll function are in a class, instead of individual parameters." still glad to see it helped you 😁
Tech Lead/Team Lead. Senior WebDev.
Intermediate Grade on Computer Systems-
High Grade on Web Application Development-
MBA (+Marketing+HHRR).
Studied a bit of law, economics and design
Location
Spain
Education
Higher Level Education Certificate on Web Application Development
Oh! nothing to do with classes it's just it receives an object but this is opinionated as I prefer to have the object structure for those "config" thingies, you can simply delete the brackets on the function declaration like that:
Nice snippet.
The new Promise callback should not be an async function. By passing an async fn to new Promise you’re “double promising”.
Refactoring to not depend on resolve/reject would solve that.
Or maybe
Tech Lead/Team Lead. Senior WebDev.
Intermediate Grade on Computer Systems-
High Grade on Web Application Development-
MBA (+Marketing+HHRR).
Studied a bit of law, economics and design
Location
Spain
Education
Higher Level Education Certificate on Web Application Development
Having it as async lets you handle the response, the error and any action to perform either it went OK or KO, so it can be customised specifically on it's environment.
This little snippet to sleep
I've never used that one xD
Love this
I'm afraid to ask where you need this one 😂😂
I've used it for polling. And... Sometimes you got to do what you got to do 😂
Hahaha no judgements. Still I let you some polling thingy I had here that may be more suitable:
poll
function is a higher-order function that returns a function,executePoll
.executePoll
function returns apromise
and will run recursively until the condition is met.Args explained:
fn
: an API request (or another async thingy that suits for this polling).validate
: Test function to see if the data matches what we want, in which case it will end the poll.interval
: To specify how much it wait between poll requests.maxAttempts
: how many tries before throwing an error.😁
I couldn't sleep last night and read this comment at 3am. Coincidentally, today, I needed this poll. Works great in my usEffect().
The only thing that caught me is that the parameters to the above poll function are in a class, instead of individual parameters.
Thanks for the code!
Didn't get the meaning of the sentence "The only thing that caught me is that the parameters to the above poll function are in a class, instead of individual parameters." still glad to see it helped you 😁
Apologies, I didn't explain correctly.
Initially, I called the poll function incorrectly as follows:
The correct method (using your code) is:
Oh! nothing to do with classes it's just it receives an object but this is opinionated as I prefer to have the object structure for those "config" thingies, you can simply delete the brackets on the function declaration like that:
and we should be good 😁
Nice snippet.
The new Promise callback should not be an async function. By passing an async fn to new Promise you’re “double promising”.
Refactoring to not depend on resolve/reject would solve that.
Or maybe
Having it as async lets you handle the response, the error and any action to perform either it went OK or KO, so it can be customised specifically on it's environment.
instead handling it as generic inside the polling function.
But sure you can tweak it as you wish to fullfill your needs 😄
This one is pretty neat, I also use it sometimes to "mock" API responses like this 👇
I love that one!