{"id":13912562,"url":"https://github.com/toniov/p-iteration","last_synced_at":"2025-04-08T00:36:49.760Z","repository":{"id":55839576,"uuid":"94317217","full_name":"toniov/p-iteration","owner":"toniov","description":"Utilities that make array iteration easy when using async/await or Promises","archived":false,"fork":false,"pushed_at":"2020-10-06T14:19:03.000Z","size":724,"stargazers_count":352,"open_issues_count":5,"forks_count":19,"subscribers_count":9,"default_branch":"master","last_synced_at":"2025-03-31T23:33:53.758Z","etag":null,"topics":["array","array-iteration","array-methods","async","async-await","async-functions","await","es2017","promise"],"latest_commit_sha":null,"homepage":"https://toniov.github.io/p-iteration","language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/toniov.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2017-06-14T10:05:53.000Z","updated_at":"2024-08-15T13:58:35.000Z","dependencies_parsed_at":"2022-08-15T07:40:48.583Z","dependency_job_id":null,"html_url":"https://github.com/toniov/p-iteration","commit_stats":null,"previous_names":["toniov/asyncitt"],"tags_count":11,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/toniov%2Fp-iteration","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/toniov%2Fp-iteration/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/toniov%2Fp-iteration/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/toniov%2Fp-iteration/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/toniov","download_url":"https://codeload.github.com/toniov/p-iteration/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":247755560,"owners_count":20990620,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["array","array-iteration","array-methods","async","async-await","async-functions","await","es2017","promise"],"created_at":"2024-08-07T01:01:33.350Z","updated_at":"2025-04-08T00:36:49.741Z","avatar_url":"https://github.com/toniov.png","language":"JavaScript","funding_links":[],"categories":["others","JavaScript"],"sub_categories":[],"readme":"# p-iteration [![Build Status](https://travis-ci.org/toniov/p-iteration.svg?branch=master)](https://travis-ci.org/toniov/p-iteration) [![NPM version](https://badge.fury.io/js/p-iteration.svg)](http://badge.fury.io/js/p-iteration)\n\n\n\u003e Make array iteration easy when using async/await and promises\n\n- Same functionality as the ES5 Array iteration methods we all know\n- All the methods return a `Promise`, making them awaitable and thenable\n- Allow the usage of async functions as callback\n- Callbacks run concurrently\n- Lightweight (no prd dependencies)\n\n\n## Install\n\n```\n$ npm install --save p-iteration\n```\n\n\n## Usage\n\nSmooth asynchronous iteration using `async/await`:\n\n```js\nconst { map } = require('p-iteration');\n\n// map passing an async function as callback\nfunction getUsers (userIds) {\n  return map(userIds, async userId =\u003e {\n    const response = await fetch(`/api/users/${userId}`);\n    return response.json();\n  });\n}\n\n// map passing a non-async function as callback\nasync function getRawResponses (userIds) {\n  const responses = await map(userIds, userId =\u003e fetch(`/api/users/${userId}`));\n  // ... do some stuff\n  return responses;\n}\n\n// ...\n```\n\n```js\nconst { filter } = require('p-iteration');\n\nasync function getFilteredUsers (userIds, name) {\n  const filteredUsers = await filter(userIds, async userId =\u003e {\n    const response = await fetch(`/api/users/${userId}`);\n    const user = await response.json();\n    return user.name === name;\n  });\n  // ... do some stuff\n  return filteredUsers;\n}\n\n// ...\n```\n\nAll methods return a Promise so they can just be used outside an async function just with plain Promises:\n\n```js\nconst { map } = require('p-iteration');\n\nmap([123, 125, 156], (userId) =\u003e fetch(`/api/users/${userId}`))\n  .then((result) =\u003e {\n    // ...\n  })\n  .catch((error) =\u003e {\n    // ...\n  });\n```\n\nIf there is a Promise in the array, it will be unwrapped before calling the callback:\n\n```js\nconst { forEach } = require('p-iteration');\nconst fetchJSON = require('nonexistent-module');\n\nfunction logUsers () {\n  const users = [\n    fetchJSON('/api/users/125'), // returns a Promise\n    { userId: 123, name: 'Jolyne', age: 19 },\n    { userId: 156, name: 'Caesar', age: 20 }\n  ];\n  return forEach(users, (user) =\u003e {\n    console.log(user);\n  });\n}\n```\n\n```js\nconst { find } = require('p-iteration');\nconst fetchJSON = require('nonexistent-module');\n\nfunction findUser (name) {\n  const users = [\n    fetchJSON('/api/users/125'), // returns a Promise\n    { userId: 123, name: 'Jolyne', age: 19 },\n    { userId: 156, name: 'Caesar', age: 20 }\n  ];\n  return find(users, (user) =\u003e user.name === name);\n}\n```\n\nThe callback will be invoked as soon as the Promise is unwrapped:\n\n```js\nconst { forEach } = require('p-iteration');\n\n// function that returns a Promise resolved after 'ms' passed\nconst delay = (ms) =\u003e new Promise(resolve =\u003e setTimeout(() =\u003e resolve(ms), ms));\n\n// 100, 200, 300 and 500 will be logged in this order\nasync function logNumbers () {\n  const numbers = [\n    delay(500),\n    delay(200),\n    delay(300),\n    100\n  ];\n  await forEach(numbers, (number) =\u003e {\n    console.log(number);\n  });\n}\n```\n\n## API\n\nThe methods are implementations of the ES5 Array iteration methods we all know with the same syntax, but all return a `Promise`. Also, with the exception of `reduce()`, all methods callbacks are run concurrently. There is a series version of each method, called: `${methodName}Series`, series methods use the same API that their respective concurrent ones.\n\nThere is a link to the [original reference](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array) of each method in the docs of this module:\n\n- [`forEach`(array, callback, [thisArg])](https://toniov.github.io/p-iteration/global.html#forEach)\n\n- [`forEachSeries`(array, callback, [thisArg])](https://toniov.github.io/p-iteration/global.html#forEachSeries)\n\n- [`map`(array, callback, [thisArg])](https://toniov.github.io/p-iteration/global.html#map)\n\n- [`mapSeries`(array, callback, [thisArg])](https://toniov.github.io/p-iteration/global.html#mapSeries)\n\n- [`find`(array, callback, [thisArg])](https://toniov.github.io/p-iteration/global.html#find)\n\n- [`findSeries`(array, callback, [thisArg])](https://toniov.github.io/p-iteration/global.html#findSeries)\n\n- [`findIndex`(array, callback, [thisArg])](https://toniov.github.io/p-iteration/global.html#findIndex)\n\n- [`findIndexSeries`(array, callback, [thisArg])](https://toniov.github.io/p-iteration/global.html#findIndexSeries)\n\n- [`some`(array, callback, [thisArg])](https://toniov.github.io/p-iteration/global.html#some)\n\n- [`someSeries`(array, callback, [thisArg])](https://toniov.github.io/p-iteration/global.html#someSeries)\n\n- [`every`(array, callback, [thisArg])](https://toniov.github.io/p-iteration/global.html#every)\n\n- [`everySeries`(array, callback, [thisArg])](https://toniov.github.io/p-iteration/global.html#everySeries)\n\n- [`filter`(array, callback, [thisArg])](https://toniov.github.io/p-iteration/global.html#filter)\n\n- [`filterSeries`(array, callback, [thisArg])](https://toniov.github.io/p-iteration/global.html#filterSeries)\n\n- [`reduce`(array, callback, [initialValue])](https://toniov.github.io/p-iteration/global.html#reduce)\n\n\n## Instance methods\n\nExtending native objects is discouraged and I don't recommend it, but in case you know what you are doing, you can extend `Array.prototype` to use the above methods as instance methods. They have been renamed as `async${MethodName}`, so the original ones are not overwritten.\n\n```js\nconst { instanceMethods } = require('p-iteration');\n\nObject.assign(Array.prototype, instanceMethods);\n\nasync function example () {\n  const foo = await [1, 2, 3].asyncMap((id) =\u003e fetch(`/api/example/${id}`));\n}\n```\n\n\n## License\n\nMIT © [Antonio V](https://github.com/toniov)","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ftoniov%2Fp-iteration","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Ftoniov%2Fp-iteration","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ftoniov%2Fp-iteration/lists"}