I see your point. This is great for hard-coded values in setTimeout() method and without need to create other constants later, like:
constdelay=setTimout(()=>{console.log('delay event')},1000);// logs 'delay event' after 1000ms each time
by wrapping setTimeout() method in a function you can re-use your defined timer function with different values each time without creating new constants:
constdelay=(fn,wait,...args)=>setTimeout(fn,wait,...args);delay(()=>{console.log('delay event 1')},1000)//logs 'delay event 1' after 1000msdelay(()=>{console.log('delay event 2')},5000)//logs 'delay event 2' after 5000ms, etc
by wrapping setTimeout() method in a function you can re-use your defined timer function with different values each time without creating new constants
I'm not sure I understand what you're saying here. They behave the same.
That's the point - they behave the same, but you don't have to create new variables for the same functionality throughout your app. Instead you just use the same function once you need timer functionality for something.
Furthermore, if you need different function signature, you can use the latter assigning it to different var.
/**
* Sleep pause.
*
* @param {Number} time The time in milliseconds.
* @return {Promise<void>}
*/constsleep=(time)=>newPromise((resolve)=>{setTimeout(resolve,time)})
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.
That number 14 really doesn't do anything. You might as well just assign the function to
delay
.I see your point. This is great for hard-coded values in setTimeout() method and without need to create other constants later, like:
by wrapping setTimeout() method in a function you can re-use your defined timer function with different values each time without creating new constants:
I'm not sure I understand what you're saying here. They behave the same.
Personally, if I were to wrap
setTimeout
it would be to change the function signature, something like this.That's the point - they behave the same, but you don't have to create new variables for the same functionality throughout your app. Instead you just use the same function once you need timer functionality for something.
Furthermore, if you need different function signature, you can use the latter assigning it to different var.
if I assign
setTimeout
that will force me to create new unnecesary variables in the entire app? And that doesn't happen if I wrap it in a function?