Giter Site home page Giter Site logo

victorarowo / consim Goto Github PK

View Code? Open in Web Editor NEW
2.0 1.0 0.0 87 KB

Consim is a small module for NodeJS, that contains utility functions which gives you more control over how asynchronous callback functions are invoked on an array.

License: MIT License

JavaScript 100.00%

consim's Introduction

Consim

NPM npm bundle size

Introduction

Consim is a small module for NodeJS, that contains utility functions which gives you more control over how asynchronous callback functions are invoked on an array.

Installation

npm install consim

Usage

Series

Executes next promise only after the preceding promise is resolved or rejected.

consim.series(arr, cb);
  • arr is an array containing all the elements to be passed into cb.
  • cb is a function that's executed for all elements in arr.
Example
const consim = require("consim");
const axios = require("axios");

consim.series([1, 2, 3, 4, 5], async num => {
  const res = await axios.get(
    `https://jsonplaceholder.typicode.com/todos/${num}`
  );
  console.log(res.data);
});

Parallel

Executes all promises at the same time. Doesn't work reliably when the input array gets large as node isn't able to handle it.

consim.parallel(arr, cb);
  • arr is an array containing all the elements to be passed into cb.
  • cb is a function that's executed for all elements in arr.
Example
const consim = require("consim");
const axios = require("axios");

consim.parallel([1, 2, 3, 4, 5], async num => {
  const res = await axios.get(
    `https://jsonplaceholder.typicode.com/todos/${num}`
  );
  console.log(res.data);
});

Batched Series

Batches array into array of arrays first, then executes each batch in parallel, but all batches in series. Useful when the input array contains a large number of elements.

consim.batchedSeries(arr, cb, elementsPerBatch);
  • arr is an array containing all the elements to be passed into cb.
  • cb is a function that's executed for all elements in arr.
  • elementsPerBatch an integer which determines how many requests run at the same time.
Example
const consim = require("consim");
const axios = require("axios");

consim.batchedSeries(
  [1, 2, 3, 4, 5],
  async num => {
    const res = await axios.get(
      `https://jsonplaceholder.typicode.com/todos/${num}`
    );
    console.log(res.data);
  },
  2
);

Tests

npm test

consim's People

Contributors

victorarowo avatar

Stargazers

Nick Durbin avatar Maaruf Dauda avatar

Watchers

James Cloos avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.