Skip to content

run a function n times, both sync and async functions are supported

License

Notifications You must be signed in to change notification settings

tjmehta/times-loop

Repository files navigation

times-loop Build Status

run a function n times, returns results as an array

Usage

Supports both ESM and CommonJS

// esm
import times from 'times-loop`
// commonjs
const times = require('times-loop').default

Simple Example

import times from 'times-loop'

var results = times(6, function (i) {
  console.log(i)
  return i
})
// prints: 0 1 2 3 4 5

console.log(results)
// prints: [0, 1, 2, 3, 4, 5]

Promise Example: Parallel

runs callbacks in parallel and returns a promise that resolves all promise results as an array

import { timesParallel } from 'times-loop'

const results = await timesParallel(6, function (i) {
  return Promise.resolve(i)
})

console.log(results)
// prints: [0, 1, 2, 3, 4, 5]

Promise Example: Series

runs callbacks in series (waits for each promise to resolve in series) and returns a promise that resolves all promise results as an array

import { timesSeries } from 'times-loop'

const results = await timesSeries(6, function (i) {
  return Promise.resolve(i)
})

console.log(results)
// prints: [0, 1, 2, 3, 4, 5]

License

MIT

About

run a function n times, both sync and async functions are supported

Resources

License

Stars

Watchers

Forks

Packages

No packages published