A functional, lightweight alternative to bluebird.js, built with
async
/await
in mind.
Features
-
Functional utility library for
async
/await
Thinklodash
for promises. -
Bluebird's powerful collections methods, built with native promises
Use functions likemap
,reduce
,filter
&some
to iterate over promises in an intuitive way. -
Fine-grained Concurrency control
Resolve all promises at once or in series of 3? the choice is yours. -
Built to support Tree Shaking from the ground up
Take what you need and leave the rest. -
Two flavors: Regular & FP style
Similar tolodash/fp
, Awaity.js comes with additional flavor to support functional composition.
If it speaks your language, tryawaity/fp
.
Introduction
Awaity.js is a subset of bluebird.js that focuses only on what's relevant for async
/ await
while using native promises instead, the result is a functional utility library with a minimal footprint. much like lodash, but for promises.
That greatly reduces the library footprint, while bluebird's is 17KB min/gzip, Awaity.js takes only 2KB for the whole lib, and since it built to support tree shaking from the ground up, you can easily pick only what's relevant to you and end up with no more than 1KB.
; const tasks = await ; console // [{...}, {...}, {...}] // Resolve a promise firstconst promise = api;const titles = await ; // Resolve an array of a promises const promisesArray = api api api;const titles = await ; // Resolve an array of a promises + map an object of promises for eachconst expendedTasks = await ; console/*[ { id: 1, title: 'Task title..', author: { ... }, comments: [{ ... }, { ... }, { ... }] }, { ... }, { ... }, { ... }]*/
Installation
npm install awaity
Usage
// Take all; // Or only what you need;;
Or for module bundles (such as webpack, parcel, or rollup), use awaity/esm
Which used ES Modules to gain tree shaking support:
;
Note: node.js
does not support ES Modules out of the box yet.
;; { const promise = fs; return ;} const directories = await ;
FP flavor
Todo: explain what it means.
FP flavor is available under the fp
submodule:
;// OR; // Just some promises that returns numbersconst promises = 123; // By ommiting the last argument,// we got a function that expects an array of promisesconst sum = ; const total = await // 6
Chaining
Awaity.js provides three different kinds of chaining to choose from:
By leveraging Promise's native chaining feature:
; const postsWithComments = await Promise
awaity/fp
:
Promise chaining + ; const postsWithComments = await Promise
flow
Using ; const postsWithComments = await ;
flow
+ awaity/fp
Using ; const postsWithComments = await
Complex example with promise chain
; const posts = await Promise ;
Complex example with flow
; const posts = await ;
API
Collections
Utilities
FP Mode
Each module also has an equivalate curried version under the fp
namespace
; // FP version is curriediteratorinitialValueiterator const sum = ; const total = await ;
Note: in FP mode, the first argument (the iterable, or promises) is always the last argument.
// Normal Mode: value is the first argument; // FP Mode: value is the last argument;