apr - async promise resolve
Collection of tools to manage control flow of/with Promises - inspired by caolan/async.
Works with and without async/await. The lib itself only uses promises.
As someone beautifully put it:
this is like caolan/async which is like lodash but async, but awaitful
contents
api
collections
You can use arrays
, objects
and iterables
as the input for all the collection functions.
concat(coll: array | Iterable | Object, iteratee: Promise): Promise
Applies iteratee
to each item in coll
, concatenating the results. Returns the concatenated list.
const readdir = ;const dirs = 'dir1' 'dir2' 'dir3'; const files = await ;
concatSeries(coll: array | Iterable | Object, iteratee: Promise): Promise
// import { series as concatSeries } from 'apr-concat/series'
concatLimit(coll: array | Iterable | Object, limit: number, iteratee: Promise): Promise
// import { limit as concatLimit } from 'apr-concat/limit'
find(coll: array | Iterable | Object, iteratee: Promise): Promise
Returns the first value in coll
that passes an async truth test.
const access = ;const files = 'file1' 'file2' 'file3'; const first = await ;
findSeries(coll: array | Iterable | Object, iteratee: Promise): Promise
// import { series as findSeries } from 'apr-find/series'
findLimit(coll: array | Iterable | Object, limit: number, iteratee: Promise): Promise
// import { limit as findLimit } from 'apr-find/limit'
forEach(coll: array | Iterable | Object, iteratee: Promise): Promise
Applies the function iteratee
to each item in coll
, in parallel.
const writeFile = ;const files = '/home/.vimrc' '/home/.zshrc'; await ;
forEachSeries(coll: array | Iterable | Object, iteratee: Promise): Promise
// import { series as forEachSeries } from 'apr-for-each/series'
forEachLimit(coll: array | Iterable | Object, limit: number, iteratee: Promise): Promise
// import { limit as forEachLimit } from 'apr-for-each/limit'
every(coll: array | Iterable | Object, iteratee: Promise): Promise
Returns true if every element in coll
satisfies an async test.
const access = ;const files = 'file1' 'file2' 'file3'; const allExist = await ;
everySeries(coll: array | Iterable | Object, iteratee: Promise): Promise
// import { series as everySeries } from 'apr-every/series'
everyLimit(coll: array | Iterable | Object, limit: number, iteratee: Promise): Promise
// import { limit as everyLimit } from 'apr-every/limit'
filter(coll: array | Iterable | Object, iteratee: Promise): Promise
Returns a new array of all the values in coll
which pass an async truth test.
const access = ;const files = 'file1' 'file2' 'file3'; var existent = await ;
filterSeries(coll: array | Iterable | Object, iteratee: Promise): Promise
// import { series as filterSeries } from 'apr-filter/series'
filterLimit(coll: array | Iterable | Object, limit: number, iteratee: Promise): Promise
// import { limit as filterLimit } from 'apr-filter/limit'
map(coll: array | Iterable | Object, iteratee: Promise): Promise
Produces a new collection of values by mapping each value in coll
through the iteratee
function.
const stat = ;const files = 'file1' 'file2' 'file3'; const stats = await ;
mapSeries(coll: array | Iterable | Object, iteratee: Promise): Promise
// import { series as mapSeries } from 'apr-map/series'
mapLimit(coll: array | Iterable | Object, limit: number, iteratee: Promise): Promise
// import { limit as mapLimit } from 'apr-map/limit'
reduce(coll: array | Iterable | Object, iteratee: Promise, initialValue: any): Promise
Reduces coll
into a single value using an async iteratee
to return each successive step.
const sum = await ;
reject(coll: array | Iterable | Object, iteratee: Promise): Promise
The opposite of filter
. Removes values that pass an async truth test.
const access = ;const files = 'file1' 'file2' 'file3'; var missing = await ;
rejectSeries(coll: array | Iterable | Object, iteratee: Promise): Promise
// import { series as rejectSeries } from 'apr-reject/series'
rejectLimit(coll: array | Iterable | Object, limit: number, iteratee: Promise): Promise
// import { limit as rejectLimit } from 'apr-reject/limit'
some(coll: array | Iterable | Object, iteratee: Promise): Promise
Returns true if at least one element in the coll
satisfies an async test.
const access = ;const files = 'file1' 'file2' 'file3'; const oneExist = await ;
someSeries(coll: array | Iterable | Object, iteratee: Promise): Promise
// import { series as someSeries } from 'apr-some/series'
someLimit(coll: array | Iterable | Object, limit: number, iteratee: Promise): Promise
// import { limit as someLimit } from 'apr-some/limit'
sortBy(coll: array | Iterable | Object, iteratee: Promise): Promise
Sorts a list by the results of running each coll
value through an async iteratee
.
const stat = ;const files = 'file1' 'file2' 'file3'; const sorted = await ;
sortBySeries(coll: array | Iterable | Object, iteratee: Promise): Promise
// import { series as sortBySeries } from 'apr-sort-by/series'
sortByLimit(coll: array | Iterable | Object, limit: number, iteratee: Promise): Promise
// import { limit as sortByLimit } from 'apr-sort-by/limit'
control flow
compose(...tasks: Array): (input: any): Promise
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())).
const then = const composed = ; const output = await ; // 7
parallel(tasks: Array | Object): Promise
Run the tasks collection of functions in parallel, without waiting until the previous function has completed.
const then = const withArray = await ; // withArray = [1, 2] const withObject = await ; // withObject = { one: 1, two: 2 }
seq(tasks: Array): (input: any): Promise
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.
const then = const seq = ; const output = await ; // 7
series(tasks: Array | Object): Promise
Run the functions in the tasks
in series, each one running once the previous function has completed.
const then = const withArray = await ; // withArray = [1, 2] const withObject = await ; // withObject = { one: 1, two: 2 }
until(test: Promise, fn: Promise): Promise
Repeatedly call fn
until test
returns true
.
const then = const maxCalls = 10;let calls = 0; const output = await
waterfall(tasks: Array | Object): Promise
Runs the tasks
array of functions in series, each passing their results to the next in the array.
const then = const output = await ; // output = 6
whilst(test: Promise, fn: Promise): Promise
Repeatedly call fn
, while test
returns true.
const then = const maxCalls = 10;let calls = 0; const output = await
utils
apply(fn: Function): Promise
Creates a continuation function with some arguments already applied.
const then = const output = await ; // output = [1, 2, 3]
asyncify(fn: Function)
Take a sync function and make it async. This is useful for plugging sync functions into a waterfall
, series
, or other async functions.
const readFile = ;const pkgPath = path; const pkg = await ;
constant(...args: any): Promise
Returns a promise that when called, then's with the values provided. Useful as the first function in a waterfall
.
const pkg = await ;
todo
- finish caolan/async feature parity
- come up with a solution for generating readme and website from source
- flow annotations
caolan/async feature parity
Eventually it should have feature parity with caolan/async.
collections
- concat
- concatSeries
- detect
- detectLimit
- detectSeries
- each
- eachLimit
- eachOf
- eachOfLimit
- eachOfSeries
- eachSeries
- every
- everyLimit
- everySeries
- filter
- filterLimit
- filterSeries
- map
- mapLimit
- mapSeries
- mapValues
- mapValuesLimit
- mapValuesSeries
- reduce
- reduceRight
- reject
- rejectLimit
- rejectSeries
- some
- someLimit
- someSeries
- sortBy
- transform
control flow
- applyEach
- applyEachSeries
- auto
- autoInject
- cargo
- compose
- doDuring
- doUntil
- doWhilst
- during
- forever
- parallel
- parallelLimit
- priorityQueue
- queue
- race
- retry
- retryable
- seq
- series
- times
- timesLimit
- timesSeries
- until
- waterfall
- whilst
utils
- apply
- asyncify
- constant
- dir
- ensureAsync
- log
- memoize
- nextTick
- reflect
- reflectAll
- setImmediate
- timeout
- unmemoize
credits
- both the method signatures and descriptions are copied from caolan/async
license
MIT