Socket
Socket
Sign inDemoInstall

p-iteration-with-concurrency

Package Overview
Dependencies
6
Maintainers
1
Versions
1
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

    p-iteration-with-concurrency

Make array iteration easy when using async/await and Promises


Version published
Weekly downloads
1
Maintainers
1
Install size
516 kB
Created
Weekly downloads
 

Readme

Source

p-iteration-with-concurrency

Make array iteration easy when using async/await and promises

  • Same functionality as the ES5 Array iteration methods we all know
  • All the methods return a Promise, making them awaitable and thenable
  • Allow the usage of async functions as callback
  • Callbacks run concurrently
  • Lightweight (no prd dependencies)

Install

$ npm install --save p-iteration-with-concurrency

Usage

Smooth asynchronous iteration using async/await:

const { map } = require('p-iteration-with-concurrency');

// map passing an async function as callback
function getUsers (userIds) {
  return map(userIds, async userId => {
    const response = await fetch(`/api/users/${userId}`);
    return response.json();
  });
}

// map passing a non-async function as callback
async function getRawResponses (userIds) {
  const responses = await map(userIds, userId => fetch(`/api/users/${userId}`));
  // ... do some stuff
  return responses;
}

// ...
const { filter } = require('p-iteration-with-concurrency');

async function getFilteredUsers (userIds, name) {
  const filteredUsers = await filter(userIds, async userId => {
    const response = await fetch(`/api/users/${userId}`);
    const user = await response.json();
    return user.name === name;
  });
  // ... do some stuff
  return filteredUsers;
}

// ...

All methods return a Promise so they can just be used outside an async function just with plain Promises:

const { map } = require('p-iteration-with-concurrency');

map([123, 125, 156], (userId) => fetch(`/api/users/${userId}`))
  .then((result) => {
    // ...
  })
  .catch((error) => {
    // ...
  });

If there is a Promise in the array, it will be unwrapped before calling the callback:

const { forEach } = require('p-iteration-with-concurrency');
const fetchJSON = require('nonexistent-module');

function logUsers () {
  const users = [
    fetchJSON('/api/users/125'), // returns a Promise
    { userId: 123, name: 'Jolyne', age: 19 },
    { userId: 156, name: 'Caesar', age: 20 }
  ];
  return forEach(users, (user) => {
    console.log(user);
  });
}
const { find } = require('p-iteration-with-concurrency');
const fetchJSON = require('nonexistent-module');

function findUser (name) {
  const users = [
    fetchJSON('/api/users/125'), // returns a Promise
    { userId: 123, name: 'Jolyne', age: 19 },
    { userId: 156, name: 'Caesar', age: 20 }
  ];
  return find(users, (user) => user.name === name);
}

The callback will be invoked as soon as the Promise is unwrapped:

const { forEach } = require('p-iteration-with-concurrency');

// function that returns a Promise resolved after 'ms' passed
const delay = (ms) => new Promise(resolve => setTimeout(() => resolve(ms), ms));

// 100, 200, 300 and 500 will be logged in this order
async function logNumbers () {
  const numbers = [
    delay(500),
    delay(200),
    delay(300),
    100
  ];
  await forEach(numbers, (number) => {
    console.log(number);
  });
}

Keywords

FAQs

Last updated on 14 Sep 2018

Did you know?

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

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc