tamesy

1.1.2 • Public • Published

tamesy

npm version Build Status ♦️ Dependency Status ♦️ devDependency Status ♦️

Tames a set of wild concurrent promises.

Installation

npm i tamesy -save

Documentation

Tamesy exposes a map function to map over a set of Promises or a iterator factory with a given concurrency.

API

Tamesy exposes a map function as its default export. The map function takes the following arguments:

  • [Function<Promise>] Array containing functions which return Promises serving as the factory (lazy).
  • Number=Infinity Maximum limit of concurrently running Promises.
  • Function Optional if passed a function invoked with each item which should return a Promise.
  • Function Optional log function if passed a function to be used for debugging (e.g. debug in examples).
  • Function Optional if passed a function providing a Promise (to overwrite native Promises e.g. O.Promise.

Tamesy returns a Promise which resolves to an Array whenever all tasks have ben run. The order of items is maintained - same as in the list of factory functions passed.

Module formats

Tamesy is built as a UMD module using webpack. The distribution version is not added to git but created as a preversion script.

  • ...ESM just import the src/index.js within your app.
  • ...CommonJS use the dist/tamesy.js
  • ...AMD use the dist/tamesy.js
  • ...<script /> link it to dist/tamesy.js or dist/tamesy.min.js

All build files are part of the npm distribution using the files array to keep install time short.

Also feel free to use unpkg.com as a CDN to the dist files.

Examples

import map from 'tamesy';
/**
 * Delay execution by time ms
 * @method delay
 * @param  {integer} time [delay in milliseconds]
 * @return {Promise}      [promise to chain into]
 */
function delay(timeout) {
    return new Promise(resolve => {
        setTimeout(resolve, timeout);
    })
}
/**
 * Queue of n items with max given
 * @method queue
 * @param  {integer} length [length of queue]
 * @param  {integer} max    [maximum integer within queue]
 * @return {Array}          [queue with items]
 */
function queue(length, max) {
    return [...new Array(length)].map(() => Math.round(Math.random() * max))
}

// Queue map'ed onto delay factories (lazy Promises)
const syncQueue = queue(10, 50)
const asyncQueue = syncQueue.map(ms => () => delay(ms))

console.huraaay = (msg, props) => console.info(`🎉 ${msg} 🍻`, props)

console.info('🏁 Starting the race 1...')
// Given a queue with 10 items and a maximum delay of 50ms
// execute two tasks concurrently without a factory function.
map(asyncQueue, 2, false, null, log).then(props => console.huraaay('All work WITHOUT iterator done! ', props))

console.info('🏁 Starting the race 2...')
// Given the same queue pipe each item of the iterable into the factory function.
map(syncQueue, 2, delay, null, log).then(props => console.huraaay('All work WITH iterator done! ', props))

Readme

Keywords

none

Package Sidebar

Install

npm i tamesy

Weekly Downloads

2

Version

1.1.2

License

MIT

Last publish

Collaborators

  • tdeekens