Lodasync
Lodasync is an asynchronous functional programing utility library heavily inspired by lodash/fp. It has no dependencies, is lightweight, has a strong focus on performance, and is fully tested and documented.
Why Lodasync?
Lodasync makes asynchronous JavaScript easier by taking the hassle out of working with promises, arrays, and asynchronous callbacks.
- Work with promises the way you work with synchronous code
- Do not re-invent the wheel and rely on fully tested and documented code
- Use functions that are performance oriented, don't waste time benchmarking yourself
const users = await
Table of contents
- Getting started
- API
- everyAsync(callback, collection)
- filterAsync(callback, collection)
- findAsync(callback, collection)
- findIndexAsync(callback, collection)
- flatMapAsync(callback, collection)
- flowAsync(...callbacks)
- forEachAsync(callback, collection)
- getAsync(path, object)
- getOrAsync(defaultValue, path, object)
- groupByAsync(callback, collection)
- mapAsync(callback, collection)
- maxByAsync(callback, collection)
- minByAsync(callback, collection)
- propsAsync(object)
- reduceAsync(callback, initialValue, collection)
- someAsync(callback, collection)
- sortByAsync(callback, collection)
- uniqByAsync(callback, collection)
Getting started
Install Lodasync using npm.
npm i lodasync
In Node.js and in a browser:
// Some async functionconst getUser = asyncid // Write async code like you write synchronous codeconst users = await // Pass promises as arguments to any methodconst users = await // Don't ❌const users = await // Do ✅ // And even array of promisesconst users = await // Don't ❌const users = await // Do ✅ // Callback arguments are always resolvedconst users = await // Don't ❌const users = await // Do ✅ // Returned array elements are always resolvedconst users = await const name = await users0name // Don't ❌const name = users0name // Do ✅ // All methods are curriedconst getUsers = const users = await // Curry is useful for chainingconst authors = await 'article-1-id' 'article-2-id' /*...*/
Note on parallelism
Callbacks are always called in parallel on all elements to maximize speed.
This also includes methods like findAsync
or findIndexAsync
, even tho their
synchronous counterpart stops on the first match.
API
everyAsync(callback, collection)
Implementation of native Array.prototype.every().
filterAsync(callback, collection)
Implementation of native Array.prototype.filter().
findAsync(callback, collection)
Implementation of native Array.prototype.find().
findIndexAsync(callback, collection)
Implementation of native Array.prototype.findIndex().
flatMapAsync(callback, collection)
Implementation of native Array.prototype.flatMap().
flowAsync(...callbacks)
Creates a function that returns the result of invoking all callbacks in series, where each successive invocation is supplied the return value of the previous.
Note that callbacks do not have to be asynchronous, you can mix synchronous and asynchronous code.
Example
const getUserIds = async /*...*/ const getUser = asyncid /*...*/ const isAuthorized = asyncuser /*...*/ const getAuthorizedUsers = const authorizedUsers = await
forEachAsync(callback, collection)
Iterate over an array in parallel.
Arguments
callback
A function that is invoked for each element. It takes the following arguments:element
The current element in the collection.index
The index of the current element in the collection.collection
The collection.
collection
The collection to iterate over.
Return value
Always returns undefined
.
Example
await
getAsync(path, object)
Gets the value at path
of object
.
Arguments
path
An array of keys, or a string that should be split on dots. When its a string,path
does not support the array notation[index]
, use the dot notation instead.index
object
The object from which to get the property.
Return value
The resolved value.
Example
const article = authors: Promise await // => 'John'await // => 'John' // Often used to get an object key of a promiseconst name = await // Don't ❌const name = await // Do ✅ // Or as an iterateeconst names = await
getOrAsync(defaultValue, path, object)
Works like getAsync
with a custom defaultValue
when the resolved value is undefined.
groupByAsync(callback, collection)
Creates an object composed of keys generated from the results of running each element of collection
thru callback
.
The order of grouped values is determined by the order they occur in collection
.
The corresponding value of each key is an array of elements responsible for generating the key.
Arguments
callback
A function that should return a key for each element. It takes the following arguments:element
The current element in the collection.index
The index of the current element in the collection.collection
The collection.
collection
The collection to iterate over.
Return value
The composed aggregate object.
Example
const getFirstName = asyncuser /*...*/ await // => { John: [john1, john2], Carol: [carol1] }
mapAsync(callback, collection)
Implementation of native Array.prototype.map().
maxByAsync(callback, collection)
Computes the maximum value of collection
by invoking callback
for each element
to generate the criterion by which the value is ranked.
Arguments
callback
A function that should return a criterion for each element. It takes the following arguments:element
The current element in the collection.index
The index of the current element in the collection.collection
The collection.
collection
The collection to iterate over.
Return value
The maximum value of collection
, undefined
if the collection is empty.
Example
const getItemPrice = asyncitem /*...*/ await // => item3
minByAsync(callback, collection)
Computes the minimum value of collection
by invoking callback
for each element
to generate the criterion by which the value is ranked.
Arguments
callback
A function that should return a criterion for each element. It takes the following arguments:element
The current element in the collection.index
The index of the current element in the collection.collection
The collection.
collection
The collection to iterate over.
Return value
The minimum value of collection
, undefined
if the collection is empty.
Example
const getItemPrice = asyncitem /*...*/ await // => item2
propsAsync(object)
Returns a promise that resolves when all values of object
are resolved.
Mostly used for parallelism.
Example
// This is run sequentiallyconst user = await const article = await const comment = await // This is run in parallelconst user article comment = await
reduceAsync(callback, initialValue, collection)
Implementation of native Array.prototype.reduce().
someAsync(callback, collection)
Implementation of native Array.prototype.some().
sortByAsync(callback, collection)
Sorts collection
by invoking callback
for each element to generate the
criterion by which the value is ranked.
This method performs a stable sort, that is, it preserves the original sort order of equal elements.
The criterion can be
- a number: using number comparison
- a string: using string comparison
- Infinity: always sorted at the end
- -Infinity: always sorted at the beginning
Arguments
callback
A function that should return a criterion for each element. It takes the following arguments:element
The current element in the collection.index
The index of the current element in the collection.collection
The collection.
collection
The collection to iterate over.
Return value
The sorted collection.
Example
const getItemPrice = asyncitem /*...*/ await // => [item2, item3, item7]
uniqByAsync(callback, collection)
Creates a duplicate-free version of collection
.
callback
is invoked for each element to generate the criterion by which uniqueness is computed.
Only the first occurrence of each element is kept.
The order of result values is determined by the order they occur in the array.
Arguments
callback
A function that should return a criterion for each element. It takes the following arguments:element
The current element in the collection.index
The index of the current element in the collection.collection
The collection.
collection
The collection to iterate over.
Return value
The new duplicate free collection.
Example
const getUserId = asyncuser /*...*/ await // => [user10, user7]