DEV Community

Mehul Lakhanpal
Mehul Lakhanpal

Posted on • Originally published at codedrops.tech

Custom util to call functions `n` times

const repeatFn = (n, fn) => {
  const results = [];
  for (let i = 0; i < n; i++) {
    results.push(fn());
  }
  return results;
};

const result = repeatFn(3, () => Math.random());
console.log(result); // [ 0.50.., 0.25.., 0.08.. ]
Enter fullscreen mode Exit fullscreen mode

Thanks for reading ๐Ÿ’™

Follow @codedrops.tech for more.

Instagram โ— Twitter โ— Facebook

Micro-Learning โ— Web Development โ— Javascript โ— MERN stack

codedrops.tech


Projects

File Ops - A VS Code extension to easily tag/alias files & quick switch between files

Top comments (0)