├── .gitignore ├── .gitattributes ├── .github ├── security.md └── workflows │ └── main.yml ├── .editorconfig ├── test.js ├── package.json ├── index.js ├── license └── readme.md /.gitignore: -------------------------------------------------------------------------------- 1 | node_modules 2 | -------------------------------------------------------------------------------- /.gitattributes: -------------------------------------------------------------------------------- 1 | * text=auto 2 | -------------------------------------------------------------------------------- /.github/security.md: -------------------------------------------------------------------------------- 1 | # Security Policy 2 | 3 | To report a security vulnerability, please use the [Tidelift security contact](https://tidelift.com/security). Tidelift will coordinate the fix and disclosure. 4 | -------------------------------------------------------------------------------- /.editorconfig: -------------------------------------------------------------------------------- 1 | root = true 2 | 3 | [*] 4 | indent_style = tab 5 | end_of_line = lf 6 | charset = utf-8 7 | trim_trailing_whitespace = true 8 | insert_final_newline = true 9 | 10 | [{package.json,*.yml}] 11 | indent_style = space 12 | indent_size = 2 13 | -------------------------------------------------------------------------------- /.github/workflows/main.yml: -------------------------------------------------------------------------------- 1 | name: CI 2 | on: 3 | - push 4 | - pull_request 5 | jobs: 6 | test: 7 | name: Node.js ${{ matrix.node-version }} 8 | runs-on: ubuntu-latest 9 | strategy: 10 | fail-fast: false 11 | matrix: 12 | node-version: 13 | - 14 14 | - 12 15 | - 10 16 | - 8 17 | - 6 18 | - 4 19 | - 0.12 20 | - 0.1 21 | steps: 22 | - uses: actions/checkout@v2 23 | - uses: actions/setup-node@v1 24 | with: 25 | node-version: ${{ matrix.node-version }} 26 | - run: npm install 27 | - run: npm test 28 | -------------------------------------------------------------------------------- /test.js: -------------------------------------------------------------------------------- 1 | import test from 'ava'; 2 | import m from './'; 3 | 4 | test.cb('async tasks will run parallelly', t => { 5 | const fixture = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]; 6 | const actual = []; 7 | 8 | m(fixture, (el, i, next) => { 9 | setTimeout(() => { 10 | actual.push(el); 11 | next(); 12 | }, Math.random() * 2000); 13 | }, () => { 14 | t.is(actual.length, fixture.length); 15 | t.notDeepEqual(actual, fixture); 16 | t.end(); 17 | }); 18 | }); 19 | 20 | test.cb('stop iteration on first error', t => { 21 | let j = 0; 22 | 23 | m([1, 2, 3], (el, i, next) => { 24 | j++; 25 | next(true); 26 | }, () => { 27 | t.is(j, 1); 28 | t.end(); 29 | }); 30 | }); 31 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "each-async", 3 | "version": "1.1.1", 4 | "description": "Async concurrent iterator (async forEach)", 5 | "license": "MIT", 6 | "repository": "sindresorhus/each-async", 7 | "author": { 8 | "name": "Sindre Sorhus", 9 | "email": "sindresorhus@gmail.com", 10 | "url": "sindresorhus.com" 11 | }, 12 | "engines": { 13 | "node": ">=0.10.0" 14 | }, 15 | "scripts": { 16 | "test": "xo && ava" 17 | }, 18 | "files": [ 19 | "index.js" 20 | ], 21 | "keywords": [ 22 | "each", 23 | "async", 24 | "asynchronous", 25 | "iteration", 26 | "iterate", 27 | "loop", 28 | "foreach", 29 | "parallel", 30 | "concurrent", 31 | "array", 32 | "flow", 33 | "control flow" 34 | ], 35 | "dependencies": { 36 | "onetime": "^1.0.0", 37 | "set-immediate-shim": "^1.0.0" 38 | }, 39 | "devDependencies": { 40 | "ava": "*", 41 | "xo": "^0.16.0" 42 | } 43 | } 44 | -------------------------------------------------------------------------------- /index.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | var onetime = require('onetime'); 3 | var setImmediateShim = require('set-immediate-shim'); 4 | 5 | module.exports = function (arr, next, cb) { 6 | var failed = false; 7 | var count = 0; 8 | 9 | cb = cb || function () {}; 10 | 11 | if (!Array.isArray(arr)) { 12 | throw new TypeError('First argument must be an array'); 13 | } 14 | 15 | if (typeof next !== 'function') { 16 | throw new TypeError('Second argument must be a function'); 17 | } 18 | 19 | var len = arr.length; 20 | 21 | if (!len) { 22 | cb(); 23 | return; 24 | } 25 | 26 | function callback(err) { 27 | if (failed) { 28 | return; 29 | } 30 | 31 | if (err !== undefined && err !== null) { 32 | failed = true; 33 | cb(err); 34 | return; 35 | } 36 | 37 | if (++count === len) { 38 | cb(); 39 | return; 40 | } 41 | } 42 | 43 | for (var i = 0; i < len; i++) { 44 | setImmediateShim(next, arr[i], i, onetime(callback, true)); 45 | } 46 | }; 47 | -------------------------------------------------------------------------------- /license: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) Sindre Sorhus (sindresorhus.com) 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in 13 | all copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 21 | THE SOFTWARE. 22 | -------------------------------------------------------------------------------- /readme.md: -------------------------------------------------------------------------------- 1 | # each-async 2 | 3 | > Async concurrent iterator (async forEach) 4 | 5 | Like [async.each()](https://github.com/caolan/async#eacharr-iterator-callback), but tiny. 6 | 7 | I often use `async.each()` for doing async operations when iterating, but I almost never use the other gadzillion methods in `async`. 8 | 9 | Async iteration is one of the most used async control flow patterns. 10 | 11 | **I would strongly recommend using promises instead. You could then use the built-in [`Promise.all()`](https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/Promise/all), or [`p-map`](https://github.com/sindresorhus/p-map) if you need concurrency control.** 12 | 13 | 14 | ## Install 15 | 16 | ``` 17 | $ npm install --save each-async 18 | ``` 19 | 20 | 21 | ## Usage 22 | 23 | ```js 24 | const eachAsync = require('each-async'); 25 | 26 | eachAsync(['foo','bar','baz'], (item, index, done) => { 27 | console.log(item, index); 28 | done(); 29 | }, error => { 30 | console.log('finished'); 31 | }); 32 | //=> 'foo 0' 33 | //=> 'bar 1' 34 | //=> 'baz 2' 35 | //=> 'finished' 36 | ``` 37 | 38 | 39 | ## API 40 | 41 | ### eachAsync(input, callback, [finishedCallback]) 42 | 43 | #### input 44 | 45 | Type: `Array` 46 | 47 | Array you want to iterate. 48 | 49 | #### callback(item, index, done) 50 | 51 | Type: `Function` 52 | 53 | Called for each item in the array with the following arguments: 54 | 55 | - `item`: the current item in the array 56 | - `index`: the current index 57 | - `done([error])`: call this when you're done with an optional error. Supplying anything other than `undefined`/`null` will stop the iteration. 58 | 59 | Note that order is not guaranteed since each item is handled concurrently. 60 | 61 | #### finishedCallback(error) 62 | 63 | Type: `Function` 64 | 65 | Called when the iteration is finished or on the first error. First argument is the error passed from `done()` in the `callback`. 66 | 67 | 68 | ## License 69 | 70 | MIT © [Sindre Sorhus](https://sindresorhus.com) 71 | --------------------------------------------------------------------------------