How to execute functions with delay, underscorejs/lodash

Background

I have a JavaScript function that prints numbers to the screen. This function prints a lot of numbers, but I am using a screen as old as Alan Turing so it can’t print the numbers so fast!

The solution? Throtlle/debounce/delay the prints using underscorejs or lodash.

Tentative

My first tentative was to use throttle.

let fun = num => {
  console.log(num);
};

let throttleFun = _.throttle(fun, 1000);

for (let i = 0; i < 10; i++) {
  throttleFun(i);
}
https://cdnjs.cloudflare.com/ajax/libs/underscore.js/1.8.3/underscore-min.js

But instead of printing all the numbers with a 1000ms of separation, it just calls the function every 1000ms, which causes it to only actually execute twice.

My next try, was to use debounce:

let fun = num => {
  console.log(num);
};

let debouncedFn = _.debounce(fun, 1000);

for (let i = 0; i < 10; i++) {
  debouncedFn(i);
}
https://cdnjs.cloudflare.com/ajax/libs/underscore.js/1.8.3/underscore-min.js

Now, again, instead of printing the values with 1000ms of separation, debounce actually delays the execution of my function since it last executed by 1000ms, which causes it to only execute once.

Question

All the behaviors I previously mentioned are documented, and they are the same for both underscore and lodash.

My question here is:

  • Using one of the previous libraries (or another similar one), is there a way to print the numbers in the console with 1000ms of separation between them, using a similar function style?


Source: stackoverflow-javascript