# delayed-loop

> function to handle async looping

Latest version **1.0.1** (published 2018-08-23) · ISC license · 0 weekly downloads

## Install

```sh
npm install delayed-loop
pnpm add delayed-loop
yarn add delayed-loop
bun add delayed-loop
```

## Health

**Score 15/100 (F)** — status: abandoned.

Positive: no vulnerabilities.

Warnings: low downloads; no types; no esm support.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 1.0.1 |
| Published | 2018-08-23 |
| First published | 2018-08-23 |
| Weekly downloads | 0 |
| License | ISC |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 0 |
| Unpacked size | 3.6 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| Author | rparham |
| Maintainers | pamblam |

## Links

- npm: https://www.npmjs.com/package/delayed-loop
- npm.io page: https://npm.io/package/delayed-loop

## Recent versions

- 1.0.1 (latest) — 2018-08-23
- 1.0.0 — 2018-08-23

## README

## Delayed Loop

A very simple function that iterates over an array asynchronously. Getting tired of copying this whole function out of my Github Gists so I'm publishing on NPM for convenience.

	/**
	 * Execute the loopBody function once for each item in the items array, 
	 * waiting for the done function (which is passed into the loopBody function)
	 * to be called before proceeding to the next item in the array.
	 * @param {Array} items - The array of items to iterate through
	 * @param {Function} loopBody - A function to execute on each item in the array.
	 *		This function is passed 3 arguments - 
	 *			1. The item in the current iteration,
	 *			2. The index of the item in the array,
	 *			3. A function to be called when the iteration may continue.
	 * @returns {Promise} - A promise that is resolved when all the items in the 
	 *		in the array have been iterated through.
	 */
	function delayedLoop(items, loopBody) {
		return new Promise(f => {
			let done = arguments[2] || f;
			let idx = arguments[3] || 0;
			let cb = items[idx + 1] ? () => delayedLoop(items, loopBody, done, idx + 1) : done;
			loopBody(items[idx], idx, cb);
		});
	}

### Usage

	const dLoop = require('delayed-loop');

	var arr = ['do','re','mi','fa','so','la','ti'];

	const loop = dLoop(arr, (itm, idx, fin)=>{
		// do something async, 
		// call fin() when ready to continue the loop
		setTimeout(()=>{
			console.log(`Item #${idx} is ${itm}`);
			fin();
		}, 1000);
	});

	// Promise resolves when all items have finished looping
	loop.then(()=>{
		console.log('done looping');
	});

---
_Source: https://npm.io/package/delayed-loop · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
