├── .gitignore ├── .travis.yml ├── LICENSE ├── README.md ├── index.js ├── package.json └── test.js /.gitignore: -------------------------------------------------------------------------------- 1 | # tmp files 2 | lib-cov 3 | *.seed 4 | *.log 5 | *.csv 6 | *.dat 7 | *.out 8 | *.pid 9 | *.gz 10 | 11 | # tmp folders 12 | pids/ 13 | logs/ 14 | results/ 15 | coverage/ 16 | 17 | # node.js 18 | node_modules/ 19 | npm-debug.log 20 | 21 | # osx 22 | .DS_Store 23 | -------------------------------------------------------------------------------- /.travis.yml: -------------------------------------------------------------------------------- 1 | node_js: 2 | - "0.10" 3 | - "0.12" 4 | - "4" 5 | sudo: false 6 | language: node_js 7 | script: "npm run test-cov" 8 | after_script: "npm install coveralls@2 && cat ./coverage/lcov.info | coveralls" 9 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2015 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 all 13 | 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 THE 21 | SOFTWARE. -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # promise-each 2 | [![NPM version][npm-image]][npm-url] 3 | [![build status][travis-image]][travis-url] 4 | [![Test coverage][coveralls-image]][coveralls-url] 5 | [![Downloads][downloads-image]][downloads-url] 6 | 7 | Call a function [for each][mdn] value in an array and return a [Promise][promise]. 8 | Should only be used for side effects. Waits for promises to resolve before 9 | proceeding to the next value. 10 | 11 | ## Installation 12 | ```bash 13 | $ npm install promise-each 14 | ``` 15 | 16 | ## Usage 17 | ```js 18 | const each = require('promise-each') 19 | 20 | Promise.resolve([1, 2, 3]) 21 | .then(each((val) => console.log(val))) 22 | // => 1 23 | // => 2 24 | // => 3 25 | ``` 26 | 27 | ## Why? 28 | This module is basically equivalent to [`bluebird.each`][bluebird], but it's 29 | handy to have the one function you need instead of a kitchen sink. Modularity! 30 | Especially handy if you're serving to the browser and need to reduce your 31 | javascript bundle size. 32 | 33 | Works great in the browser with 34 | [browserify](http://github.com/substack/node-browserify)! 35 | 36 | ## See Also 37 | - [promise-every](https://github.com/yoshuawuyts/promise-every) 38 | - [promise-filter](https://github.com/yoshuawuyts/promise-filter) 39 | - [promise-map](https://github.com/yoshuawuyts/promise-map) 40 | - [promise-reduce](https://github.com/yoshuawuyts/promise-reduce) 41 | - [promise-some](https://github.com/yoshuawuyts/promise-some) 42 | 43 | ## License 44 | [MIT](https://tldrlegal.com/license/mit-license) 45 | 46 | [npm-image]: https://img.shields.io/npm/v/promise-each.svg?style=flat-square 47 | [npm-url]: https://npmjs.org/package/promise-each 48 | [travis-image]: https://img.shields.io/travis/yoshuawuyts/promise-each.svg?style=flat-square 49 | [travis-url]: https://travis-ci.org/yoshuawuyts/promise-each 50 | [coveralls-image]: https://img.shields.io/coveralls/yoshuawuyts/promise-each.svg?style=flat-square 51 | [coveralls-url]: https://coveralls.io/r/yoshuawuyts/promise-each?branch=master 52 | [downloads-image]: http://img.shields.io/npm/dm/promise-each.svg?style=flat-square 53 | [downloads-url]: https://npmjs.org/package/promise-each 54 | 55 | [mdn]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/each 56 | [promise]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise 57 | [bluebird]: https://github.com/petkaantonov/bluebird/blob/master/API.md#eachfunction-iterator---promise 58 | -------------------------------------------------------------------------------- /index.js: -------------------------------------------------------------------------------- 1 | const Promise = require('any-promise') 2 | const assert = require('assert') 3 | 4 | module.exports = each 5 | 6 | // apply a function to all values 7 | // should only be used for side effects 8 | // (fn) -> prom 9 | function each (fn) { 10 | assert.equal(typeof fn, 'function') 11 | return function (arr) { 12 | arr = Array.isArray(arr) ? arr : [arr] 13 | 14 | return arr.reduce(function (prev, curr, i) { 15 | return prev.then(function () { return fn(curr, i, arr.length) }) 16 | }, Promise.resolve()).then(function () { return arr }) 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "promise-each", 3 | "version": "2.2.0", 4 | "description": "Call a function for each value in an array and return a Promise", 5 | "main": "index.js", 6 | "scripts": { 7 | "test": "standard && NODE_ENV=test tape test.js", 8 | "test-cov": "NODE_ENV=test istanbul cover test.js" 9 | }, 10 | "repository": "yoshuawuyts/promise-each", 11 | "keywords": [ 12 | "bluebird", 13 | "each", 14 | "native", 15 | "promise" 16 | ], 17 | "license": "MIT", 18 | "dependencies": { 19 | "any-promise": "^0.1.0" 20 | }, 21 | "devDependencies": { 22 | "bluebird": "^2.8.2", 23 | "istanbul": "^0.3.5", 24 | "standard": "^4.5.2", 25 | "tape": "^3.4.0" 26 | }, 27 | "files": [ 28 | "LICENSE", 29 | "README.md", 30 | "index.js" 31 | ] 32 | } 33 | -------------------------------------------------------------------------------- /test.js: -------------------------------------------------------------------------------- 1 | const Promise = require('any-promise') 2 | const test = require('tape') 3 | const each = require('./') 4 | 5 | test('promise-each should assert input types', function (t) { 6 | t.plan(1) 7 | t.throws(each.bind(null, 123)) 8 | }) 9 | 10 | test('should call a fn for each value in arr', function (t) { 11 | t.plan(3) 12 | 13 | var indexes = [] 14 | var lengths = [] 15 | var values = [] 16 | 17 | Promise.resolve([1, 2, 3]) 18 | .then(each(eachFn)) 19 | .then(checkFn) 20 | .catch(handleErr) 21 | 22 | function eachFn (val, i, length) { 23 | indexes.push(i) 24 | lengths.push(length) 25 | return values.push(val) 26 | } 27 | 28 | function checkFn (val) { 29 | t.deepEqual(indexes, [0, 1, 2]) 30 | t.deepEqual(lengths, [3, 3, 3]) 31 | t.deepEqual(values, [1, 2, 3]) 32 | } 33 | 34 | function handleErr () { 35 | t.fail('catch') 36 | } 37 | }) 38 | 39 | test('should call n when n=1', function (t) { 40 | t.plan(1) 41 | 42 | Promise.resolve(1) 43 | .then(each(eachFn)) 44 | .then(function () {t.pass('ok')}) 45 | 46 | function eachFn () { 47 | return arguments 48 | } 49 | }) 50 | 51 | test('should return the original array unmodified', function (t) { 52 | t.plan(1) 53 | 54 | const arr = [1, 2, 3] 55 | 56 | Promise.resolve(arr) 57 | .then(each(eachFn)) 58 | .then(function (arg) {t.deepEqual(arg, arr)}) 59 | 60 | function eachFn () { 61 | return arguments 62 | } 63 | }) 64 | 65 | test('should wait for promises to be resolved', function (t) { 66 | t.plan(1) 67 | 68 | var arr = [] 69 | 70 | Promise.resolve([late, early, check]) 71 | .then(each(eachFn)) 72 | 73 | function eachFn (fn) { 74 | const val = fn() 75 | return val 76 | } 77 | 78 | function early () { 79 | arr.push('early') 80 | } 81 | 82 | function late () { 83 | return new Promise(function (resolve) { 84 | setTimeout(function () { 85 | arr.push('late') 86 | resolve() 87 | }, 50) 88 | }) 89 | } 90 | 91 | function check () { 92 | t.deepEqual(arr, ['late', 'early']) 93 | } 94 | }) 95 | --------------------------------------------------------------------------------