New Case Study:See how Anthropic automated 95% of dependency reviews with Socket.Learn More
Socket
Sign inDemoInstall
Socket

p-iteration

Package Overview
Dependencies
Maintainers
1
Versions
10
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

p-iteration

Make array iteration easy when using async/await and Promises

1.1.8
latest
Source
npm
Version published
Weekly downloads
518K
3.39%
Maintainers
1
Weekly downloads
 
Created

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

Keywords

async/await

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