Socket
Book a DemoInstallSign in
Socket

chunkify

Package Overview
Dependencies
Maintainers
1
Versions
21
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

chunkify

A functional API to unblock your JavaScript thread.

Source
npmnpm
Version
2.1.1
Version published
Weekly downloads
11K
18.39%
Maintainers
1
Weekly downloads
 
Created
Source

chunkify

Build Status

An ES6-developed functional API that prevents long-running scripts from blocking the JavaScript thread.

Introduction

This is an API for getting long-running JavaScripts to periodically unblock the thread. The idea is to use timeouts to chunk up work and let the call stack unwind.

Install

$ npm install --save chunkify

Usage

Import the module:

import chunkify from 'chunkify'

Options

In API methods, an optional options object may provide any subset of the following data:

  • chunk: the number times to successively invoke fn before yielding control of the main thread.
    • default value is 1
    • must be positive
    • aliases: chunksize
  • delay: the minimal time in milliseconds to wait before continuing to invoke fn.
    • default value is 0
    • must be non-negative
    • aliases: yield, yieldtime, delaytime
  • scope: the object on which fn is invoked on.
    • default value is null
    • must not be a Number, Boolean, or undefined

API

Arrays

chunkify.each(Array array, Function fn, [Object options])

fn is invoked on successive array elements and their indices (fn(item, index)).

Returns a Promise that resolves with undefined when fn has been invoked on all items in array.

chunkify.map(Array array, Function fn, [Object options])

Identical to chunkify.each, except the returned Promise resolves with the array mapped by fn.

chunkify.reduce(Array array, Function fn, [Object options])

Exactly like the native reduce on Array.prototype, but the work is chunked up as above, and the returned promise resolves with the result of the reduction.

If any invocation of fn throws an Error, the returned promise is rejected with an object {error, item, index}, where error is the caught Error, index is the index in array where the invocation threw, and item is array[index]. No further processing happens after the failure.

Loops

chunkify.range(Function fn, Number final, [Object options])

Invoke fn in chunks from the range options.start to final. If options.start is given, it must be a Number less than or equal to final. Its default value is 0.

Returns a Promise that resolves with undefined when the entire range has been traversed.

chunkify.loop(Function fn, Number range, [Object options])

Like chunkify.range, with options.start forced to 0.

If any invocation of fn throws an Error, the promise is rejected with an object {error, index}, where error is the caught Error and index is the index in array where the invocation threw. No further processing happens after the failure.

Contributing

Development is in snake_case ES6.

Get the source.

$ git clone git@github.com:yangmillstheory/chunkify

Install dependencies.

$ npm install

Compile sources.

$ node_modules/.bin/gulp

Run tests.

$ npm test

License

MIT © 2015, Victor Alvarez

Keywords

gulp

FAQs

Package last updated on 27 Sep 2015

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