Comparing version 0.1.2 to 0.2.0
{ | ||
"name": "promiso", | ||
"version": "0.1.2", | ||
"version": "0.2.0", | ||
"description": "Powerful promise utilities for any JS environment", | ||
@@ -5,0 +5,0 @@ "main": "lib/index.js", |
@@ -19,21 +19,68 @@ # Promiso | ||
## Use | ||
### Standard promises | ||
```js | ||
const promiso = require('promiso'); | ||
// You can also import individual methods. | ||
// Double a number after 1 second. | ||
const slowDouble = (number) => { | ||
return new Promise((resolve) => { | ||
setTimeout(() => resolve(number * 2), 1000); | ||
}); | ||
}; | ||
const numbers = [1, 2, 3, 4]; | ||
promiso.mapLimit(numbers, 2, slowDouble) | ||
.then((doubled) => { | ||
// 4 items limited to 2 concurrent executions, so this should fire after about 2 seconds. | ||
console.log(doubled); // [2, 4, 6, 8] | ||
}); | ||
``` | ||
### `async`/`await` | ||
```js | ||
const promiso = require('promiso'); | ||
// Helper function for async/await. | ||
const sleep = (time) => new Promise((resolve) => setTimeout(resolve, time)); | ||
// Triple a number after 1 second. | ||
const slowTriple = async (number) => { | ||
await sleep(1000); | ||
return number * 3; | ||
}; | ||
const numbers = [1, 2, 3, 4, 5, 6]; | ||
const main = async () => { | ||
const tripled = await promiso.mapLimit(numbers, 3, slowTriple); | ||
// 6 items limited to 3 concurrent executions, so this should complete after about 2 seconds. | ||
console.log(tripled); // [3, 6, 9, 12, 15, 18] | ||
}; | ||
main(); | ||
``` | ||
### Individual functions | ||
You can also import individual functions for smaller bundles. | ||
```js | ||
const parallelLimit = require('promiso/lib/parallelLimit'); | ||
``` | ||
## Currently supported methods | ||
## Currently supported functions | ||
**Actual docs coming soon :-)** | ||
*See [caolan/async docs](https://caolan.github.io/async/docs.html), but an | ||
"async function" in Promiso is a function that returns a promise (or any | ||
`then`-able), rather than a function that accepts a callback. The methods | ||
themselves do not accept callbacks, but they instead return promises that | ||
resolve with the result.* | ||
*See [caolan/async docs](https://caolan.github.io/async/docs.html), but an "async function" in | ||
Promiso is a function that returns a promise (including | ||
[ES2017's true async functions][async-function]), rather than a function that accepts a callback. | ||
The library functions themselves do not accept callbacks; they instead return promises that | ||
resolve or reject accordingly.* | ||
* [`map`](https://caolan.github.io/async/docs.html#map) | ||
* [`mapLimit`](https://caolan.github.io/async/docs.html#mapLimit) | ||
* [`parallel`](https://caolan.github.io/async/docs.html#parallel) | ||
* [`parallelLimit`](https://caolan.github.io/async/docs.html#parallelLimit) | ||
* [`series`](https://caolan.github.io/async/docs.html#series) | ||
@@ -45,1 +92,2 @@ [build-badge]: https://travis-ci.org/AndyBarron/promiso.svg?branch=master | ||
[promise-polyfill]: https://github.com/stefanpenner/es6-promise#auto-polyfill | ||
[async-function]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/async_function |
import map = require('./map'); | ||
import mapLimit = require('./mapLimit'); | ||
import parallel = require('./parallel'); | ||
import parallelLimit = require('./parallelLimit'); | ||
import series = require('./series'); | ||
export = { | ||
map, | ||
mapLimit, | ||
parallel, | ||
parallelLimit, | ||
series, | ||
}; |
import parallelLimit = require('./parallelLimit'); | ||
import { AsyncFunction } from './types'; | ||
import { AsyncSupplier } from './types'; | ||
export = <T>(tasks: Array<AsyncFunction<T>>) => parallelLimit(tasks, Infinity); | ||
export = <T>(tasks: Array<AsyncSupplier<T>>) => parallelLimit(tasks, Infinity); |
@@ -1,2 +0,2 @@ | ||
import { AsyncFunction } from './types'; | ||
import { AsyncSupplier } from './types'; | ||
@@ -12,6 +12,6 @@ const startThread = async <T> (taskConfigs: Array<TaskConfig<T>>, | ||
export = <T>(tasks: Array<AsyncFunction<T>>, limit = Infinity): Promise<Array<T>> => { | ||
export = <T>(tasks: Array<AsyncSupplier<T>>, limit = Infinity): Promise<Array<T>> => { | ||
const numThreads = Math.max(1, Math.min(tasks.length, limit)); | ||
const handle: CancelHandle = {}; | ||
const configs = tasks.map((task, index) => ({task, index})); | ||
const configs = tasks.map((task, index) => ({task, index})).reverse(); // Threads run in reverse | ||
const results: Array<T> = []; | ||
@@ -36,3 +36,3 @@ const threadPromises: Array<Promise<void>> = []; | ||
index: number; | ||
task: AsyncFunction<T>; | ||
task: AsyncSupplier<T>; | ||
} |
export type AsyncFunction<T> = () => Promise<T>; | ||
export type AsyncFunction<A, B> = (value: A) => Promise<B>; | ||
export type AsyncSupplier<T> = () => Promise<T>; | ||
export type Collection<T> = Array<T>; |
const index = require('../src/index'); | ||
it('should export all methods', () => { | ||
expect(Object.keys(index).length).toBe(2); | ||
expect(Object.keys(index).length).toBe(5); | ||
}); |
const parallel = require('../src/parallel'); | ||
const { createSleepTasks, createSleepTasksAndStats, sleep } = require('./utils'); | ||
const { createSleepTasks, createSleepTasksAndStats } = require('./utils'); | ||
@@ -4,0 +4,0 @@ it('should run all tasks at once', async () => { |
const parallelLimit = require('../src/parallelLimit'); | ||
const { createSleepTasks, createSleepTasksAndStats, sleep } = require('./utils'); | ||
const { createSleepTasks, createSleepTasksAndStats } = require('./utils'); | ||
@@ -4,0 +4,0 @@ it('should limit max running tasks', async () => { |
const sleep = (seconds) => new Promise((resolve) => setTimeout(resolve, seconds * 1000)); | ||
const createSleepItemsAndStats = (count, duration = 0.01) => { | ||
const stats = { | ||
active: 0, | ||
maxActive: 0, | ||
}; | ||
const f = async (item) => { | ||
stats.active++; | ||
stats.maxActive = Math.max(stats.active, stats.maxActive); | ||
await sleep(duration); | ||
stats.active--; | ||
return item; | ||
}; | ||
const items = []; | ||
for (let i = 0; i < count; i++) { | ||
items.push(i); | ||
} | ||
return { f, items, stats }; | ||
}; | ||
const createSleepTasksAndStats = (count, duration = 0.01) => { | ||
@@ -28,2 +47,3 @@ const stats = { | ||
module.exports = { | ||
createSleepItemsAndStats, | ||
createSleepTasks, | ||
@@ -30,0 +50,0 @@ createSleepTasksAndStats, |
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
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
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
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
46
92
23994
357