🚀 Big News: Socket Acquires Coana to Bring Reachability Analysis to Every Appsec Team.Learn more

p-iteration

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install
p

p-iteration

Make array iteration easy when using async/await and Promises

1.1.8
latest
100

Supply Chain Security

100

Vulnerability

100

Quality

76

Maintenance

100

License

Version published
Weekly downloads
465K
-9.04%
Maintainers
1
Weekly downloads
 
Created
Issues
5

What is p-iteration?

The p-iteration npm package provides utility functions for working with asynchronous iterables in a more convenient and readable manner. It allows you to perform operations like map, forEach, filter, and reduce on promises and async functions, making it easier to handle asynchronous control flow.

What are p-iteration's main functionalities?

map

The `map` function allows you to apply an asynchronous function to each element in an array and returns a new array with the results. This is useful for transforming data asynchronously.

const pIteration = require('p-iteration');

async function asyncMapExample() {
  const numbers = [1, 2, 3, 4, 5];
  const results = await pIteration.map(numbers, async (num) => {
    return num * 2;
  });
  console.log(results); // [2, 4, 6, 8, 10]
}

asyncMapExample();

forEach

The `forEach` function allows you to perform an asynchronous operation on each element in an array without returning a new array. This is useful for side effects like logging or updating a database.

const pIteration = require('p-iteration');

async function asyncForEachExample() {
  const numbers = [1, 2, 3, 4, 5];
  await pIteration.forEach(numbers, async (num) => {
    console.log(num);
  });
}

asyncForEachExample();

filter

The `filter` function allows you to apply an asynchronous predicate function to each element in an array and returns a new array with only the elements that pass the predicate. This is useful for filtering data asynchronously.

const pIteration = require('p-iteration');

async function asyncFilterExample() {
  const numbers = [1, 2, 3, 4, 5];
  const results = await pIteration.filter(numbers, async (num) => {
    return num % 2 === 0;
  });
  console.log(results); // [2, 4]
}

asyncFilterExample();

reduce

The `reduce` function allows you to apply an asynchronous reducer function to each element in an array, accumulating the results into a single value. This is useful for aggregating data asynchronously.

const pIteration = require('p-iteration');

async function asyncReduceExample() {
  const numbers = [1, 2, 3, 4, 5];
  const result = await pIteration.reduce(numbers, async (acc, num) => {
    return acc + num;
  }, 0);
  console.log(result); // 15
}

asyncReduceExample();

Other packages similar to p-iteration

FAQs

Package last updated on 15 Mar 2019

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts