Skip to content

FizzyGalacticus/chunky-promise

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

7 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

chunky-promise

chunky-promise offers the ability to split up operations/function calls over data into chunks. For example, this is very useful if you need to make network/api requests for each entry in a large data set. This can be used to perform synchronous operations, but do keep in mind that chunky-promise does return a Promise object.

Installation

yarn add @fizzygalacticus/chunky-promise || npm i --save @fizzygalacticus/chunky-promise

Usage

chunky-promise accepts four arguments:

  1. arr: the array to perform your operations over
  2. chunkSize: your desired size of chunks to break arr into
  3. op: function to operate on data. Has signature (val, idx) => any
  4. handler: (optional) - handle results from op as you go, instead of after all have completed.

note if you specify a handler method, the resulting array from chunky-promise will be empty

Without handler

const chunkyPromise = require('@fizzygalacticus/chunky-promise');

const myData = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];

const res = await chunkyPromise(myData, 2, (val, idx) => ({ [val]: idx }));

console.log(res);

outputs:

[
    { '1': 0 },
    { '2': 1 },
    { '3': 2 },
    { '4': 3 },
    { '5': 4 },
    { '6': 5 },
    { '7': 6 },
    { '8': 7 },
    { '9': 8 },
    { '10': 9 },
]

With handler

const chunkyPromise = require('@fizzygalacticus/chunky-promise');

const myData = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];

const res = await chunkyPromise(myData, 2, (val, idx) => ({ [val]: idx }), console.log);

console.log(res); // empty array

outputs:

[ { '1': 0 }, { '2': 1 } ]
[ { '3': 2 }, { '4': 3 } ]
[ { '5': 4 }, { '6': 5 } ]
[ { '7': 6 }, { '8': 7 } ]
[ { '9': 8 }, { '10': 9 } ]
[]

About

Asynchronous data operations in chunks

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published