Skip to content

Ohar/forEachTimeout

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

42 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

NPM version License: MIT Node version install size

Run callback on every array element and return array of callback results with promise

Goal

Sometimes you need to run handler on array with timeouts. For example when you parse web pages you need to pause before getting next page to prevent server from shutting down. That is why forEachTimeout is developed.

Example usage

forEachTimeout(
  [1, 2, 5, 'foo'], // Array to handle
  e => Promise.resolve(e + ' bar'), // Handler function
  1000 // Timeout in ms between runs
)
.then( // After 4+ seconds
  results => {
    console.info('results', results); // ['1 bar', '2 bar', '5 bar', 'foo bar']
  }
);

How to install

npm install foreach-timeout

Syntax

forEachTimeout(Array elements, Function handler [, Number timeout]) => Promise.resolve(results)

elements

Type: Array

Array with elements to handle

handler

Type: Function(element, Number index, Array elements)

Function to handle each element. Should return some result of handling.

timeout

Type: Number

Default: 1000

Time in ms to pause between handle next element.

results

Type: Array

Array with returned results of handling each element.

About

Run callback on every array element and return array of callback results with promise

Topics

Resources

License

Stars

Watchers

Forks

Packages

No packages published