Comparing version 1.0.0 to 1.1.0
37
index.js
@@ -15,4 +15,8 @@ 'use strict'; | ||
function isObject(o) { | ||
return o instanceof Object; | ||
} | ||
function isGenerator(v) { | ||
return typeof v === 'object' && v !== null && isFunction(v.next) && isFunction(v.throw); | ||
return isObject(v) && isFunction(v.next) && isFunction(v.throw); | ||
} | ||
@@ -36,12 +40,29 @@ | ||
isThenable(v) ? thenableToFunction(v) : | ||
Array.isArray(v) ? collectionToFunction(v) : | ||
Array.isArray(v) ? arrayToFunction(v) : | ||
isObject(v) ? objectToFunction(v) : | ||
cb => process.nextTick(() => cb(null, v)); | ||
} | ||
function collectionToFunction(col) { | ||
function arrayToFunction(arr) { | ||
return iterableToFunction( | ||
() => new Array(arr.length), | ||
cb => arr.forEach(cb), | ||
count => count === arr.length); | ||
} | ||
function objectToFunction(o) { | ||
const keys = Object.keys(o); | ||
return iterableToFunction( | ||
() => Object.assign({}, o), | ||
cb => keys.forEach(k => cb(o[k], k)), | ||
count => count === keys.length); | ||
} | ||
function iterableToFunction(createDest, iterate, hasFinished) { | ||
return function (cb) { | ||
const values = Array(col.length); | ||
const dest = createDest(); | ||
let count = 0; | ||
let stopIteration = false; | ||
col.forEach(function (e, i) { | ||
iterate(function (e, i) { | ||
elementToFunction(e)(function (err, value) { | ||
@@ -51,6 +72,6 @@ if (stopIteration) { | ||
} | ||
values[i] = value; | ||
dest[i] = value; | ||
count += 1; | ||
if (err || count === col.length) { | ||
cb(err, values); | ||
if (err || hasFinished(count)) { | ||
cb(err, dest); | ||
stopIteration = true; | ||
@@ -57,0 +78,0 @@ } |
{ | ||
"name": "few", | ||
"version": "1.0.0", | ||
"version": "1.1.0", | ||
"description": "Write asynchronous code in a synchronous fashion", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
@@ -1,2 +0,2 @@ | ||
# few | ||
# few [![Build Status](https://travis-ci.org/forter/few.svg?branch=master)](https://travis-ci.org/forter/few) | ||
Write fewer lines of code by turning node-style asynchronous functions and promises to a synchronous code using ES6 generators. | ||
@@ -12,3 +12,2 @@ ## Requirements | ||
```javascript | ||
const assert = require('assert'); | ||
const few = require('few'); | ||
@@ -24,3 +23,6 @@ | ||
// Multiple invocations of few run asynchronously | ||
few(function* () { | ||
// Yield or delegate directly | ||
const a = yield cb => returnValue(1, cb); | ||
@@ -31,3 +33,9 @@ const b = yield Promise.resolve(2); | ||
const all = yield [ | ||
// Prints 1 2 3 4 | ||
console.log(a, b, c, d); | ||
}); | ||
few(function* () { | ||
// Parallelize using arrays | ||
const arr = yield [ | ||
cb => returnValue(1, cb), | ||
@@ -39,4 +47,18 @@ Promise.resolve(2), | ||
assert.deepEqual([a, b, c, d], all); | ||
// Prints [ 1, 2, 3, 4 ] | ||
console.log(arr); | ||
}); | ||
few(function* () { | ||
// Parallelize using objects | ||
const obj = yield { | ||
a: cb => returnValue(1, cb), | ||
b: Promise.resolve(2), | ||
c: 3, | ||
d: generateValue(4) | ||
}; | ||
// Prints { a: 1, b: 2, c: 3, d: 4 } | ||
console.log(obj); | ||
}); | ||
``` | ||
@@ -56,6 +78,7 @@ ## Usage | ||
- Arrays combining thunks, promises, generators or simple values to be run in parallel | ||
- Objects containing thunks, promises, generators or simple values to be run in parallel | ||
### Parallelization | ||
few allows parallelization by yielding an array. | ||
The array may contain any combination of: | ||
few allows parallelization by yielding an array or an object. | ||
The yielded object or array may contain any combination of: | ||
- Single node-style callback argument functions (aka thunks) | ||
@@ -66,3 +89,3 @@ - Promises | ||
When all given elements have finished processing, a new array that contains the results of the given elements in the same order will be returned. | ||
When all given elements have finished processing, a new object or array that contains the results of the given elements in the same order will be returned. | ||
If any of the elements provides an error, the error will be thrown inside the generator. | ||
@@ -69,0 +92,0 @@ |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
19235
14
320
119