Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/sergioramos/apr
this is like caolan/async which is like lodash but async, but awaitful
https://github.com/sergioramos/apr
async async-await async-programming control-flow es2015 javascript promises
Last synced: 29 days ago
JSON representation
this is like caolan/async which is like lodash but async, but awaitful
- Host: GitHub
- URL: https://github.com/sergioramos/apr
- Owner: sergioramos
- License: mit
- Created: 2016-05-11T01:34:38.000Z (over 8 years ago)
- Default Branch: master
- Last Pushed: 2022-12-06T19:49:33.000Z (about 2 years ago)
- Last Synced: 2024-09-30T09:03:55.956Z (4 months ago)
- Topics: async, async-await, async-programming, control-flow, es2015, javascript, promises
- Language: JavaScript
- Homepage: https://apr.js.org
- Size: 2.43 MB
- Stars: 74
- Watchers: 5
- Forks: 5
- Open Issues: 67
-
Metadata Files:
- Readme: readme.md
- License: LICENSE
Awesome Lists containing this project
README
## apr
[![Travis](https://img.shields.io/travis/ramitos/apr.svg?style=flat-square)](https://travis-ci.org/ramitos/apr) [![](https://img.shields.io/codeclimate/coverage/github/ramitos/apr.svg?style=flat-square)](https://codeclimate.com/github/ramitos/apr/coverage)
Collection of tools to manage control flow of/with Promises - inspired by [caolan/async](https://github.com/caolan/async).
Works with and without async/await. The lib itself only uses promises.
As someone beautifully put it:
> this is like [caolan/async](https://github.com/caolan/async) which is like [lodash](https://github.com/lodash/lodash) but async, but awaitful
* [Collections](#collections)
* [concat](#concat)
* [every](#every)
* [filter](#filter)
* [find](#find)
* [for-each](#for-each)
* [map](#map)
* [reduce](#reduce)
* [reject](#reject)
* [some](#some)
* [sort-by](#sort-by)
* [Control Flow](#control-flow)
* [compose](#compose)
* [parallel](#parallel)
* [seq](#seq)
* [series](#series)
* [until](#until)
* [waterfall](#waterfall)
* [whilst](#whilst)
* [Utilities](#utilities)
* [apply](#apply)
* [asyncify](#asyncify)
* [awaitify](#awaitify)
* [constant](#constant)
* [intercept](#intercept)
* [reflect](#reflect)
* [times](#times)
* [main](#main)
* [credits](#credits)
* [license](#license)
## Collections
Functions for manipulating collections, such as arrays and objects.
## concat
[packages/concat/index.js:30-35](https://github.com/ramitos/apr/blob/f07fd61f41a1053a0efb60ed54f393b83b73a7f5/packages/concat/index.js#L30-L35 "Source code on GitHub")
Applies `iteratee` to each item in `coll`, concatenating the results. Returns the concatenated list.[![](https://img.shields.io/npm/v/apr-concat.svg?style=flat-square)](https://www.npmjs.com/package/apr-concat) [![](https://img.shields.io/npm/l/apr-concat.svg?style=flat-square)](https://www.npmjs.com/package/apr-concat)
**Parameters**
- `input` **([Array](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Array) \| [Object](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Object) | Iterable)**
- `iteratee` **[Function](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Statements/function)****Examples**
```javascript
import awaitify from 'apr-awaitify';
import concat from 'apr-concat';const readdir = awaitify(fs.readdir);
const dirs = [
'dir1',
'dir2',
'dir3'
];const files = await concat(dirs, async (dir) =>
await readdir(dir)
);
```Returns **[Promise](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Promise)**
### series
[packages/concat/series.js:11-11](https://github.com/ramitos/apr/blob/f07fd61f41a1053a0efb60ed54f393b83b73a7f5/packages/concat/series.js#L11-L11 "Source code on GitHub")
**Parameters**
- `input` **([Array](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Array) \| [Object](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Object) | Iterable)**
- `iteratee` **[Function](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Statements/function)**Returns **[Promise](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Promise)**
### limit
[packages/concat/limit.js:13-23](https://github.com/ramitos/apr/blob/f07fd61f41a1053a0efb60ed54f393b83b73a7f5/packages/concat/limit.js#L13-L23 "Source code on GitHub")
**Parameters**
- `input` **([Array](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Array) \| [Object](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Object) | Iterable)**
- `limit` **[Number](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Number)**
- `iteratee` **[Function](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Statements/function)**Returns **[Promise](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Promise)**
## every
[packages/every/index.js:30-35](https://github.com/ramitos/apr/blob/f07fd61f41a1053a0efb60ed54f393b83b73a7f5/packages/every/index.js#L30-L35 "Source code on GitHub")
Returns true if every element in `coll` satisfies an async test.[![](https://img.shields.io/npm/v/apr-every.svg?style=flat-square)](https://www.npmjs.com/package/apr-every) [![](https://img.shields.io/npm/l/apr-every.svg?style=flat-square)](https://www.npmjs.com/package/apr-every)
**Parameters**
- `input` **([Array](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Array) \| [Object](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Object) | Iterable)**
- `iteratee` **[Function](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Statements/function)****Examples**
```javascript
import awaitify from 'apr-awaitify';
import every from 'apr-every';const access = awaitify(fs.access);
const files = [
'file1',
'file2',
'file3'
];const allExist = await every(files, async (file) =>
await access(file)
);
```Returns **[Promise](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Promise)**
### series
[packages/every/series.js:11-11](https://github.com/ramitos/apr/blob/f07fd61f41a1053a0efb60ed54f393b83b73a7f5/packages/every/series.js#L11-L11 "Source code on GitHub")
**Parameters**
- `input` **([Array](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Array) \| [Object](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Object) | Iterable)**
- `iteratee` **[Function](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Statements/function)**Returns **[Promise](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Promise)**
### limit
[packages/every/limit.js:14-24](https://github.com/ramitos/apr/blob/f07fd61f41a1053a0efb60ed54f393b83b73a7f5/packages/every/limit.js#L14-L24 "Source code on GitHub")
**Parameters**
- `input` **([Array](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Array) \| [Object](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Object) | Iterable)**
- `limit` **[Number](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Number)**
- `iteratee` **[Function](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Statements/function)**Returns **[Promise](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Promise)**
## filter
[packages/filter/index.js:31-31](https://github.com/ramitos/apr/blob/f07fd61f41a1053a0efb60ed54f393b83b73a7f5/packages/filter/index.js#L31-L31 "Source code on GitHub")
Returns a new array of all the values in `coll` which pass an async truth test.[![](https://img.shields.io/npm/v/apr-filter.svg?style=flat-square)](https://www.npmjs.com/package/apr-filter) [![](https://img.shields.io/npm/l/apr-filter.svg?style=flat-square)](https://www.npmjs.com/package/apr-filter)
**Parameters**
- `input` **([Array](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Array) \| [Object](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Object) | Iterable)**
- `iteratee` **[Function](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Statements/function)****Examples**
```javascript
import awaitify from 'apr-awaitify';
import filter from 'apr-filter';const access = awaitify(fs.access);
const files = [
'file1',
'file2',
'file3'
];var existent = await filter(files, async (file) =>
await access(file)
);
```Returns **[Promise](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Promise)**
### series
[packages/filter/series.js:11-11](https://github.com/ramitos/apr/blob/f07fd61f41a1053a0efb60ed54f393b83b73a7f5/packages/filter/series.js#L11-L11 "Source code on GitHub")
**Parameters**
- `input` **([Array](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Array) \| [Object](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Object) | Iterable)**
- `iteratee` **[Function](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Statements/function)**Returns **[Promise](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Promise)**
### limit
[packages/filter/limit.js:13-14](https://github.com/ramitos/apr/blob/f07fd61f41a1053a0efb60ed54f393b83b73a7f5/packages/filter/limit.js#L13-L14 "Source code on GitHub")
**Parameters**
- `input` **([Array](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Array) \| [Object](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Object) | Iterable)**
- `limit` **[Number](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Number)**
- `iteratee` **[Function](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Statements/function)**Returns **[Promise](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Promise)**
## find
[packages/find/index.js:30-35](https://github.com/ramitos/apr/blob/f07fd61f41a1053a0efb60ed54f393b83b73a7f5/packages/find/index.js#L30-L35 "Source code on GitHub")
Returns the first value in `coll` that passes an async truth test.[![](https://img.shields.io/npm/v/apr-find.svg?style=flat-square)](https://www.npmjs.com/package/apr-find) [![](https://img.shields.io/npm/l/apr-find.svg?style=flat-square)](https://www.npmjs.com/package/apr-find)
**Parameters**
- `input` **([Array](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Array) \| [Object](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Object) | Iterable)**
- `iteratee` **[Function](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Statements/function)****Examples**
```javascript
import awaitify from 'apr-awaitify';
import find from 'apr-find';const access = awaitify(fs.access);
const files = [
'file1',
'file2',
'file3'
];const first = await find(files, async (file) =>
await access(file)
);
```Returns **[Promise](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Promise)**
### series
[packages/find/series.js:11-11](https://github.com/ramitos/apr/blob/f07fd61f41a1053a0efb60ed54f393b83b73a7f5/packages/find/series.js#L11-L11 "Source code on GitHub")
**Parameters**
- `input` **([Array](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Array) \| [Object](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Object) | Iterable)**
- `iteratee` **[Function](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Statements/function)**Returns **[Promise](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Promise)**
### limit
[packages/find/limit.js:13-23](https://github.com/ramitos/apr/blob/f07fd61f41a1053a0efb60ed54f393b83b73a7f5/packages/find/limit.js#L13-L23 "Source code on GitHub")
**Parameters**
- `input` **([Array](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Array) \| [Object](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Object) | Iterable)**
- `limit` **[Number](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Number)**
- `iteratee` **[Function](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Statements/function)**Returns **[Promise](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Promise)**
## for-each
[packages/for-each/index.js:29-34](https://github.com/ramitos/apr/blob/f07fd61f41a1053a0efb60ed54f393b83b73a7f5/packages/for-each/index.js#L29-L34 "Source code on GitHub")
Applies the function `iteratee` to each item in `coll`, in parallel.[![](https://img.shields.io/npm/v/apr-for-each.svg?style=flat-square)](https://www.npmjs.com/package/apr-for-each) [![](https://img.shields.io/npm/l/apr-for-each.svg?style=flat-square)](https://www.npmjs.com/package/apr-for-each)
**Parameters**
- `input` **([Array](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Array) \| [Object](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Object) | Iterable)**
- `iteratee` **[Function](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Statements/function)****Examples**
```javascript
import awaitify from 'apr-awaitify';
import forEach from 'apr-for-each';const writeFile = awaitify(fs.writeFile);
const files = [
'/home/.vimrc',
'/home/.zshrc'
];await forEach(files, async (file) =>
await writeFile(file, 'boom')
);
```Returns **[Promise](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Promise)**
### series
[packages/for-each/series.js:11-11](https://github.com/ramitos/apr/blob/f07fd61f41a1053a0efb60ed54f393b83b73a7f5/packages/for-each/series.js#L11-L11 "Source code on GitHub")
**Parameters**
- `input` **([Array](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Array) \| [Object](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Object) | Iterable)**
- `iteratee` **[Function](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Statements/function)**Returns **[Promise](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Promise)**
### limit
[packages/for-each/limit.js:13-23](https://github.com/ramitos/apr/blob/f07fd61f41a1053a0efb60ed54f393b83b73a7f5/packages/for-each/limit.js#L13-L23 "Source code on GitHub")
**Parameters**
- `input` **([Array](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Array) \| [Object](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Object) | Iterable)**
- `limit` **[Number](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Number)**
- `iteratee` **[Function](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Statements/function)**Returns **[Promise](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Promise)**
## map
[packages/map/index.js:30-35](https://github.com/ramitos/apr/blob/f07fd61f41a1053a0efb60ed54f393b83b73a7f5/packages/map/index.js#L30-L35 "Source code on GitHub")
Produces a new collection of values by mapping each value in `coll` through the `iteratee` function.[![](https://img.shields.io/npm/v/apr-map.svg?style=flat-square)](https://www.npmjs.com/package/apr-map) [![](https://img.shields.io/npm/l/apr-map.svg?style=flat-square)](https://www.npmjs.com/package/apr-map)
**Parameters**
- `input` **([Array](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Array) \| [Object](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Object) | Iterable)**
- `iteratee` **[Function](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Statements/function)****Examples**
```javascript
import awaitify from 'apr-awaitify';
import map from 'apr-map';const stat = awaitify(fs.stat);
const files = [
'file1',
'file2',
'file3'
];const stats = await map(files, async (file) =>
await stat(file);
);
```Returns **[Promise](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Promise)**
### series
[packages/map/series.js:11-11](https://github.com/ramitos/apr/blob/f07fd61f41a1053a0efb60ed54f393b83b73a7f5/packages/map/series.js#L11-L11 "Source code on GitHub")
**Parameters**
- `input` **([Array](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Array) \| [Object](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Object) | Iterable)**
- `iteratee` **[Function](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Statements/function)**Returns **[Promise](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Promise)**
### limit
[packages/map/limit.js:13-23](https://github.com/ramitos/apr/blob/f07fd61f41a1053a0efb60ed54f393b83b73a7f5/packages/map/limit.js#L13-L23 "Source code on GitHub")
**Parameters**
- `input` **([Array](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Array) \| [Object](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Object) | Iterable)**
- `limit` **[Number](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Number)**
- `iteratee` **[Function](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Statements/function)**Returns **[Promise](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Promise)**
## reduce
[packages/reduce/index.js:23-33](https://github.com/ramitos/apr/blob/f07fd61f41a1053a0efb60ed54f393b83b73a7f5/packages/reduce/index.js#L23-L33 "Source code on GitHub")
Reduces `coll` into a single value using an async `iteratee` to return each successive step.[![](https://img.shields.io/npm/v/apr-reduce.svg?style=flat-square)](https://www.npmjs.com/package/apr-reduce) [![](https://img.shields.io/npm/l/apr-reduce.svg?style=flat-square)](https://www.npmjs.com/package/apr-reduce)
**Parameters**
- `input` **([Array](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Array) \| [Object](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Object) | Iterable)**
- `iteratee` **[Function](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Statements/function)****Examples**
```javascript
import reduce from 'apr-reduce';const sum = await reduce([1, 2, 3], async (sum, item) =>
new Promise((resolve) => resolve(sum + item))
);
```Returns **[Promise](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Promise)**
## reject
[packages/reject/index.js:31-31](https://github.com/ramitos/apr/blob/f07fd61f41a1053a0efb60ed54f393b83b73a7f5/packages/reject/index.js#L31-L31 "Source code on GitHub")
The opposite of [`filter`](#filter). Removes values that pass an async truth test.[![](https://img.shields.io/npm/v/apr-reject.svg?style=flat-square)](https://www.npmjs.com/package/apr-reject) [![](https://img.shields.io/npm/l/apr-reject.svg?style=flat-square)](https://www.npmjs.com/package/apr-reject)
**Parameters**
- `input` **([Array](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Array) \| [Object](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Object) | Iterable)**
- `iteratee` **[Function](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Statements/function)****Examples**
```javascript
import awaitify from 'apr-awaitify';
import reject from 'apr-reject';const access = awaitify(fs.access);
const files = [
'file1',
'file2',
'file3'
];var missing = await reject(files, async (file) =>
await access(file)
);
```Returns **[Promise](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Promise)**
### series
[packages/reject/series.js:11-11](https://github.com/ramitos/apr/blob/f07fd61f41a1053a0efb60ed54f393b83b73a7f5/packages/reject/series.js#L11-L11 "Source code on GitHub")
**Parameters**
- `input` **([Array](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Array) \| [Object](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Object) | Iterable)**
- `iteratee` **[Function](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Statements/function)**Returns **[Promise](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Promise)**
### limit
[packages/reject/limit.js:13-14](https://github.com/ramitos/apr/blob/f07fd61f41a1053a0efb60ed54f393b83b73a7f5/packages/reject/limit.js#L13-L14 "Source code on GitHub")
**Parameters**
- `input` **([Array](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Array) \| [Object](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Object) | Iterable)**
- `limit` **[Number](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Number)**
- `iteratee` **[Function](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Statements/function)**Returns **[Promise](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Promise)**
## some
[packages/some/index.js:31-31](https://github.com/ramitos/apr/blob/f07fd61f41a1053a0efb60ed54f393b83b73a7f5/packages/some/index.js#L31-L31 "Source code on GitHub")
Returns true if at least one element in the `coll` satisfies an async test.[![](https://img.shields.io/npm/v/apr-some.svg?style=flat-square)](https://www.npmjs.com/package/apr-some) [![](https://img.shields.io/npm/l/apr-some.svg?style=flat-square)](https://www.npmjs.com/package/apr-some)
**Parameters**
- `input` **([Array](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Array) \| [Object](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Object) | Iterable)**
- `iteratee` **[Function](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Statements/function)****Examples**
```javascript
import awaitify from 'apr-awaitify';
import some from 'apr-some';const access = awaitify(fs.access);
const files = [
'file1',
'file2',
'file3'
];const oneExist = await some(files, async (file) =>
await access(file)
);
```Returns **[Promise](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Promise)**
### series
[packages/some/series.js:11-11](https://github.com/ramitos/apr/blob/f07fd61f41a1053a0efb60ed54f393b83b73a7f5/packages/some/series.js#L11-L11 "Source code on GitHub")
**Parameters**
- `input` **([Array](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Array) \| [Object](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Object) | Iterable)**
- `iteratee` **[Function](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Statements/function)**Returns **[Promise](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Promise)**
### series
[packages/sort-by/series.js:11-11](https://github.com/ramitos/apr/blob/f07fd61f41a1053a0efb60ed54f393b83b73a7f5/packages/sort-by/series.js#L11-L11 "Source code on GitHub")
**Parameters**
- `input` **([Array](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Array) \| [Object](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Object) | Iterable)**
- `iteratee` **[Function](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Statements/function)**Returns **[Promise](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Promise)**
### limit
[packages/some/limit.js:13-13](https://github.com/ramitos/apr/blob/f07fd61f41a1053a0efb60ed54f393b83b73a7f5/packages/some/limit.js#L13-L13 "Source code on GitHub")
**Parameters**
- `input` **([Array](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Array) \| [Object](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Object) | Iterable)**
- `limit` **[Number](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Number)**
- `iteratee` **[Function](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Statements/function)**Returns **[Promise](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Promise)**
## sort-by
[packages/sort-by/index.js:32-32](https://github.com/ramitos/apr/blob/f07fd61f41a1053a0efb60ed54f393b83b73a7f5/packages/sort-by/index.js#L32-L32 "Source code on GitHub")
Sorts a list by the results of running each `coll` value through an async `iteratee`.[![](https://img.shields.io/npm/v/apr-sort-by.svg?style=flat-square)](https://www.npmjs.com/package/apr-sort-by) [![](https://img.shields.io/npm/l/apr-sort-by.svg?style=flat-square)](https://www.npmjs.com/package/apr-sort-by)
**Parameters**
- `input` **([Array](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Array) \| [Object](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Object) | Iterable)**
- `iteratee` **[Function](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Statements/function)****Examples**
```javascript
import awaitify from 'apr-awaitify';
import sortBy from 'apr-sort-by';const stat = awaitify(fs.stat);
const files = [
'file1',
'file2',
'file3'
];const sorted = await sortBy(files, await (file) => {
const { mtime } = await stat(file);
return mtime;
});
```Returns **[Promise](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Promise)**
### limit
[packages/sort-by/limit.js:13-14](https://github.com/ramitos/apr/blob/f07fd61f41a1053a0efb60ed54f393b83b73a7f5/packages/sort-by/limit.js#L13-L14 "Source code on GitHub")
**Parameters**
- `input` **([Array](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Array) \| [Object](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Object) | Iterable)**
- `limit` **[Number](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Number)**
- `iteratee` **[Function](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Statements/function)**Returns **[Promise](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Promise)**
## Control Flow
A collection of async functions for controlling the flow through a script.
## compose
[packages/compose/index.js:28-31](https://github.com/ramitos/apr/blob/f07fd61f41a1053a0efb60ed54f393b83b73a7f5/packages/compose/index.js#L28-L31 "Source code on GitHub")
Creates a function which is a composition of the passed asynchronous functions. Each function consumes the return value of the function that follows. Composing functions f(), g(), and h() would produce the result of f(g(h())).[![](https://img.shields.io/npm/v/apr-compose.svg?style=flat-square)](https://www.npmjs.com/package/apr-compose) [![](https://img.shields.io/npm/l/apr-compose.svg?style=flat-square)](https://www.npmjs.com/package/apr-compose)
**Parameters**
- `function` **[Function](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Statements/function)**
**Examples**
```javascript
import compose from 'apr-compose';const then = (v) => new Promise((resolve) => resolve(v));
const composed = compose(
async (v) => await then(v + 1),
async (v) => await then(v + 2),
async (v) => await then(v + 3)
);const output = await composed(1); // 7
```Returns **[Function](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Statements/function)**
## parallel
[packages/parallel/index.js:34-46](https://github.com/ramitos/apr/blob/f07fd61f41a1053a0efb60ed54f393b83b73a7f5/packages/parallel/index.js#L34-L46 "Source code on GitHub")
Run the tasks collection of functions in parallel, without waiting until the previous function has completed.[![](https://img.shields.io/npm/v/apr-parallel.svg?style=flat-square)](https://www.npmjs.com/package/apr-parallel) [![](https://img.shields.io/npm/l/apr-parallel.svg?style=flat-square)](https://www.npmjs.com/package/apr-parallel)
**Parameters**
- `tasks` **([Array](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Array)<[Promise](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Promise)> | [Object](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Object))**
**Examples**
```javascript
import parallel from 'apr-parallel';const then = (v) => new Promise((resolve) => resolve(v));
const withArray = await parallel([
async () => await then(1),
async () => await then(2)
]);// withArray = [1, 2]
const withObject = await parallel({
one: async () => await then(1),
two: async () => await then(2)
});// withObject = { one: 1, two: 2 }
```Returns **[Promise](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Promise)**
## seq
[packages/seq/index.js:27-27](https://github.com/ramitos/apr/blob/f07fd61f41a1053a0efb60ed54f393b83b73a7f5/packages/seq/index.js#L27-L27 "Source code on GitHub")
Version of the compose function that is more natural to read. Each function consumes the return value of the previous function. It is the equivalent of compose with the arguments reversed.[![](https://img.shields.io/npm/v/apr-seq.svg?style=flat-square)](https://www.npmjs.com/package/apr-seq) [![](https://img.shields.io/npm/l/apr-seq.svg?style=flat-square)](https://www.npmjs.com/package/apr-seq)
**Parameters**
- `tasks` **...[Function](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Statements/function)**
**Examples**
```javascript
import seq from 'apr-seq';const then = (v) => new Promise((resolve) => resolve(v));
const seq = seq(
async (v) => await then(v + 1),
async (v) => await then(v + 2),
async (v) => await then(v + 3)
);const output = await seq(1); // 7
```Returns **[Function](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Statements/function)**
## series
[packages/series/index.js:34-46](https://github.com/ramitos/apr/blob/f07fd61f41a1053a0efb60ed54f393b83b73a7f5/packages/series/index.js#L34-L46 "Source code on GitHub")
Run the functions in the `tasks` in series, each one running once the previous function has completed.[![](https://img.shields.io/npm/v/apr-series.svg?style=flat-square)](https://www.npmjs.com/package/apr-series) [![](https://img.shields.io/npm/l/apr-series.svg?style=flat-square)](https://www.npmjs.com/package/apr-series)
**Parameters**
- `tasks` **([Array](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Array)<[Function](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Statements/function)> | [Object](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Object))**
**Examples**
```javascript
import series from 'apr-series';const then = (v) => new Promise((resolve) => resolve(v));
const withArray = await series([
async () => await then(1),
async () => await then(2)
]);// withArray = [1, 2]
const withObject = await series({
one: async () => await then(1),
two: async () => await then(2)
});// withObject = { one: 1, two: 2 }
```Returns **[Promise](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Promise)**
## until
[packages/until/index.js:33-38](https://github.com/ramitos/apr/blob/f07fd61f41a1053a0efb60ed54f393b83b73a7f5/packages/until/index.js#L33-L38 "Source code on GitHub")
Repeatedly call `fn` until `test` returns `true`.[![](https://img.shields.io/npm/v/apr-until.svg?style=flat-square)](https://www.npmjs.com/package/apr-until) [![](https://img.shields.io/npm/l/apr-until.svg?style=flat-square)](https://www.npmjs.com/package/apr-until)
**Parameters**
- `test` **[Function](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Statements/function)**
- `fn` **[Function](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Statements/function)****Examples**
```javascript
import until from 'apr-until';const then = (v) => new Promise((resolve) => resolve(v));
const maxCalls = 10;
let calls = 0;const output = await until(async () => {
await then();
return (calls += 1) >= maxCalls;
}, async () => (
await then(calls)
);// output = 10
```Returns **[Promise](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Promise)**
## waterfall
[packages/waterfall/index.js:28-41](https://github.com/ramitos/apr/blob/f07fd61f41a1053a0efb60ed54f393b83b73a7f5/packages/waterfall/index.js#L28-L41 "Source code on GitHub")
Runs the `tasks` array of functions in series, each passing their results to the next in the array.[![](https://img.shields.io/npm/v/apr-waterfall.svg?style=flat-square)](https://www.npmjs.com/package/apr-waterfall) [![](https://img.shields.io/npm/l/apr-waterfall.svg?style=flat-square)](https://www.npmjs.com/package/apr-waterfall)
**Parameters**
- `tasks` **([Array](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Array)<[Function](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Statements/function)> | [Object](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Object))**
- `initial` **Any?****Examples**
```javascript
import waterfall from 'apr-waterfall';const then = (v) => new Promise((resolve) => resolve(v));
const output = await waterfall([
async () => await then(1),
async (v) => await then(v + 2),
async (v) => await then(v + 3)
]);// output = 6
```Returns **[Promise](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Promise)**
## whilst
[packages/whilst/index.js:32-37](https://github.com/ramitos/apr/blob/f07fd61f41a1053a0efb60ed54f393b83b73a7f5/packages/whilst/index.js#L32-L37 "Source code on GitHub")
Repeatedly call `fn`, while `test` returns true.[![](https://img.shields.io/npm/v/apr-whilst.svg?style=flat-square)](https://www.npmjs.com/package/apr-whilst) [![](https://img.shields.io/npm/l/apr-whilst.svg?style=flat-square)](https://www.npmjs.com/package/apr-whilst)
**Parameters**
- `test` **[Function](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Statements/function)**
- `fn` **[Function](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Statements/function)****Examples**
```javascript
import whilst from 'apr-whilst';const then = (v) => new Promise((resolve) => resolve(v));
const maxCalls = 10;
let calls = 0;const output = await whilst(async () => {
await then();
return (calls += 1) < maxCalls;
}, async () => (
await then(calls)
);// output = 10
```Returns **[Promise](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Promise)**
## Utilities
A collection of awaitable utility functions.
## apply
[packages/apply/index.js:27-27](https://github.com/ramitos/apr/blob/f07fd61f41a1053a0efb60ed54f393b83b73a7f5/packages/apply/index.js#L27-L27 "Source code on GitHub")
Creates a continuation function with some arguments already applied.[![](https://img.shields.io/npm/v/apr-apply.svg?style=flat-square)](https://www.npmjs.com/package/apr-apply) [![](https://img.shields.io/npm/l/apr-apply.svg?style=flat-square)](https://www.npmjs.com/package/apr-apply)
**Parameters**
- `function` **[Function](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Statements/function)**
- `arguments` **...Any****Examples**
```javascript
import parallel from 'apr-parallel';
import apply from 'apr-apply';const then = (v) => new Promise((resolve) => resolve(v));
const output = await parallel([
apply(then, 1)
apply(then, 2)
apply(then, 3)
]);// output = [1, 2, 3]
```Returns **[Function](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Statements/function)**
## asyncify
[packages/asyncify/index.js:26-33](https://github.com/ramitos/apr/blob/f07fd61f41a1053a0efb60ed54f393b83b73a7f5/packages/asyncify/index.js#L26-L33 "Source code on GitHub")
Take a sync function and make it async. This is useful for plugging sync functions into a [`waterfall`](#waterfall), [`series`](#series), or other async functions.[![](https://img.shields.io/npm/v/apr-asyncify.svg?style=flat-square)](https://www.npmjs.com/package/apr-asyncify) [![](https://img.shields.io/npm/l/apr-asyncify.svg?style=flat-square)](https://www.npmjs.com/package/apr-asyncify)
**Parameters**
- `function` **[Function](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Statements/function)**
**Examples**
```javascript
import awaitify from 'apr-awaitify';
import asyncify from 'apr-asyncify';
import waterfall from 'apr-waterfall';
import apply from 'apr-apply';const readFile = awaitify(require('fs').readFile);
const pkgPath = path.join(__dirname, './package.json');const pkg = await waterfall([
apply(readFile, pkgPath, 'utf8'),
asyncify(JSON.parse)
]);
```Returns **[Function](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Statements/function)**
## awaitify
[packages/awaitify/index.js:22-25](https://github.com/ramitos/apr/blob/f07fd61f41a1053a0efb60ed54f393b83b73a7f5/packages/awaitify/index.js#L22-L25 "Source code on GitHub")
Transform a callback-based function into a promise-based one.[![](https://img.shields.io/npm/v/apr-awaitify.svg?style=flat-square)](https://www.npmjs.com/package/apr-awaitify) [![](https://img.shields.io/npm/l/apr-awaitify.svg?style=flat-square)](https://www.npmjs.com/package/apr-awaitify)
**Parameters**
- `function` **[Function](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Statements/function)**
**Examples**
```javascript
import { readFile as readFileCb } from 'fs';
import awaitify from 'apr-awaitify';
import path from 'path';const readFile = awaitify(readFileCb);
const pkgPath = path.join(__dirname, './package.json');const pkg = await readFile(pkgPath, 'utf-8');
```Returns **[Function](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Statements/function)**
## constant
[packages/constant/index.js:22-22](https://github.com/ramitos/apr/blob/f07fd61f41a1053a0efb60ed54f393b83b73a7f5/packages/constant/index.js#L22-L22 "Source code on GitHub")
Returns a promise that when called, then's with the values provided. Useful as the first function in a [`waterfall`](#waterfall).[![](https://img.shields.io/npm/v/apr-constant.svg?style=flat-square)](https://www.npmjs.com/package/apr-constant) [![](https://img.shields.io/npm/l/apr-constant.svg?style=flat-square)](https://www.npmjs.com/package/apr-constant)
**Parameters**
- `arguments` **...any**
**Examples**
```javascript
import asyncify from 'apr-asyncify';
import waterfall from 'apr-waterfall';
import constant from 'apr-constant';const pkg = await waterfall([
constant('{"name": "apr"}'),
asyncify(JSON.parse)
]);
```Returns **[Promise](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Promise)**
## intercept
[packages/intercept/index.js:19-25](https://github.com/ramitos/apr/blob/f07fd61f41a1053a0efb60ed54f393b83b73a7f5/packages/intercept/index.js#L19-L25 "Source code on GitHub")
Intercepts errors, the Go way![![](https://img.shields.io/npm/v/apr-intercept.svg?style=flat-square)](https://www.npmjs.com/package/apr-intercept) [![](https://img.shields.io/npm/l/apr-intercept.svg?style=flat-square)](https://www.npmjs.com/package/apr-intercept)
**Parameters**
- `input` **[Promise](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Promise)**
**Examples**
```javascript
import ctch from 'apr-intercept';const [err1, res1] = await ctch(fn(1));
const [err2, res2] = await ctch(fn(1));
const [, res3] = await ctch(fn(3));
```Returns **[Promise](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Promise)**
## reflect
[packages/reflect/index.js:28-37](https://github.com/ramitos/apr/blob/f07fd61f41a1053a0efb60ed54f393b83b73a7f5/packages/reflect/index.js#L28-L37 "Source code on GitHub")
Wraps the function in another function that always returns data even when it errors.
The object returned has either the property error or value.[![](https://img.shields.io/npm/v/apr-reflect.svg?style=flat-square)](https://www.npmjs.com/package/apr-reflect) [![](https://img.shields.io/npm/l/apr-reflect.svg?style=flat-square)](https://www.npmjs.com/package/apr-reflect)
**Parameters**
- `input` **[Function](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Statements/function)**
**Examples**
```javascript
import parallel from 'apr-parallel';
import reflect from 'apr-reflect';const then = (v) => new Promise((resolve) => resolve(v));
const res = await parallel([
async () => {
throw new Error('heyo')
},
async () => await then(2)
]);// res = [{ error: Error('heyo'), value: null }, { error: null, value: 2 }]
```Returns **[Function](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Statements/function)**
## times
[packages/times/index.js:24-24](https://github.com/ramitos/apr/blob/f07fd61f41a1053a0efb60ed54f393b83b73a7f5/packages/times/index.js#L24-L24 "Source code on GitHub")
Calls the `iteratee` function `n` times, and accumulates results in the same manner you would use with [map](#map).[![](https://img.shields.io/npm/v/apr-times.svg?style=flat-square)](https://www.npmjs.com/package/apr-times) [![](https://img.shields.io/npm/l/apr-times.svg?style=flat-square)](https://www.npmjs.com/package/apr-times)
**Parameters**
- `n` **[Number](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Number)**
- `iteratee` **[Function](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Statements/function)****Examples**
```javascript
import times from 'apr-times';const then = (v) => new Promise((resolve) => resolve(v));
const res = await times(6, async (i) =>
await then(i);
);// res = [0, 1, 2, 3, 4, 5]
```Returns **[Promise](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Promise)**
### series
[packages/times/series.js:11-11](https://github.com/ramitos/apr/blob/f07fd61f41a1053a0efb60ed54f393b83b73a7f5/packages/times/series.js#L11-L11 "Source code on GitHub")
**Parameters**
- `n` **[Number](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Number)**
- `iteratee` **[Function](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Statements/function)**Returns **[Promise](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Promise)**
### limit
[packages/times/limit.js:13-23](https://github.com/ramitos/apr/blob/f07fd61f41a1053a0efb60ed54f393b83b73a7f5/packages/times/limit.js#L13-L23 "Source code on GitHub")
**Parameters**
- `n` **[Number](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Number)**
- `limit` **[Number](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Number)**
- `iteratee` **[Function](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Statements/function)**Returns **[Promise](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Promise)**
## main
[packages/main/index.js:27-27](https://github.com/ramitos/apr/blob/f07fd61f41a1053a0efb60ed54f393b83b73a7f5/packages/main/index.js#L27-L27 "Source code on GitHub")
Catches a promise error, writes the stacktrace to stderr and exists[![](https://img.shields.io/npm/v/apr-main.svg?style=flat-square)](https://www.npmjs.com/package/apr-main) [![](https://img.shields.io/npm/l/apr-main.svg?style=flat-square)](https://www.npmjs.com/package/apr-main)
**Parameters**
- `input` **[Promise](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Promise)**
**Examples**
```javascript
import main from 'apr-main';main(async () => 'hello') // writes nothing
main(async () => undefined) // writes nothing
main(async () => { throw new Error('uncaught error') }) // writes the stack trace to stderr and exists
```Returns **[Promise](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Promise)**
## credits
- both the method signatures and descriptions are copied from [caolan/async](https://github.com/caolan/async/blob/master/LICENSE)
## license
MIT