DEV Community

Discussion on: 24 modern ES6 code snippets to solve practical JS problems

Collapse
 
vonheikemen profile image
Heiker
const delay = (fn, wait, ...args) => setTimeout(fn, wait, ...args);

That number 14 really doesn't do anything. You might as well just assign the function to delay.

const delay = setTimeout;
Collapse
 
madza profile image
Madza • Edited

I see your point. This is great for hard-coded values in setTimeout() method and without need to create other constants later, like:

const delay = 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:

const delay = (fn, wait, ...args) => setTimeout(fn, wait, ...args);
delay(()=>{console.log('delay event 1')}, 1000) //logs 'delay event 1' after 1000ms
delay(()=>{console.log('delay event 2')}, 5000) //logs 'delay event 2' after 5000ms, etc
Collapse
 
vonheikemen profile image
Heiker • Edited

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.

var delay = (fn, wait, ...args) => setTimeout(fn, wait, ...args);
var renamed = setTimeout;

var handler = (arg) => { console.log('delay event 1', arg) };
var other_handler = (arg) => { console.log('another one', arg) };

delay(handler, 1000, 'from delay');
renamed(handler, 1000, 'from renamed');
setTimeout(handler, 1000, 'from setTimeout');

delay(other_handler, 2000, 'from delay');
renamed(other_handler, 2000, 'from renamed');
setTimeout(other_handler, 2000, 'from setTimeout');

Personally, if I were to wrap setTimeout it would be to change the function signature, something like this.

var delay_fn = (wait, fn) => (...args) => setTimeout(fn, wait, ...args);

var delay_log = delay_fn(1000, handler);

delay_log('from delay log');

I only use var because I tested these on firefox's devtools.

Thread Thread
 
madza profile image
Madza

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.

Thread Thread
 
vonheikemen profile image
Heiker

they behave the same, but you don't have to create new variables for the same functionality throughout your app.

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?

Thread Thread
 
piterden profile image
Denis Efremov
/**
 * Sleep pause.
 *
 * @param {Number} time The time in milliseconds.
 * @return {Promise<void>}
 */
const sleep = (time) => new Promise((resolve) => {
  setTimeout(resolve, time)
})