rg-async
A small library with JS functional utility methods to run async code with promises (async/await keywords)
Installation
$ npm install --save rg-async
$ npm i -S rg-async
Test
$ npm t
Basic usage
- Without async/await keywords:
const rgAsync = ; rgAsync // output => [2] ;
- With async/await keywords:
const rgAsync = ; rgAsync // output => [2,4,6];
- With async method scope:
const rgAsync = ;const array = 123; { await rgAsync;} // output => rg-async 1, rg-async 2, rg-async 3, All promises resolved ;
API
Filter
-
filter(srcArray, predicate)
method invokes in parallel an asyncpredicate
function on each item in the given source Array. -
This will return a
promise
to be resolved containing the new array with the items that predicate function returned a truthy value. -
The async
predicate
function follows thestandard javascript filter arguments
-(value, index, array)
and needs to return apromise
. -
Examples
- With
.then
-.catch
functions
const rgAsync = ;rgAsync// output => [1,2]; // if exists any case that you throw an error on your predicate function- With
async/await
keywords
const rgAsync = ;// if you are inside of a async function scope// if exist any case that you throw an error you should wrap this with try-catch clausetryconst result = await rgAsync;console; // output => [1,2]catcherrconsole; - With
Map
-
map(srcArray, mapper)
method invokes in parallel an asyncmapper
con function on each item in the given source Array. -
This will return a
promise
to be resolved containing the new array with the mapped/transformed items. -
The
mapper
function follows thestandard javascript map arguments
-(value, index, array)
and needs to return apromise
. -
Examples
- With
.then
-.catch
functions
const rgAsync = ;rgAsync// output => [2,4,6]; // if exists any case that you throw an error on your mapper function- With
async/await
keywords
const rgAsync = ;// if you are inside of a async function scope// if exist any case that you throw an error you should wrap this with try-catch clausetryconst result = await rgAsync;console; // output => [2,4,6]catcherrconsole; - With
Each
-
each(srcArray, consumer)
method invokes in parallel an asyncconsumer
function on each item in the given source Array. -
This will return a
promise
without any resolved value. -
The
consumer
function follows thestandard javascript forEach arguments
-(value, index, array)
and needs to return apromise
. -
Examples
- With
.then
-.catch
functions
const rgAsync = ;rgAsync// output => 1,2,3,done; // if exists a case that you throw an error on your consumer function- With
async/await
keywords
const rgAsync = ;// if you are inside of a async function scope// if exist any case that you throw an error you should wrap this with try-catch clausetryawait rgAsync;console; // output => 1,2,3,donecatcherrconsole; - With
Reduce
-
reduce(srcArray, reducer, accumulator)
method invokes in series an asyncreducer
function on each item in the given source Array. -
The
reducer
function transforms anaccumulator
value based on each item iterated over. Thereducer
function follows thestandard javascript map arguments
-(accumulator, currValue, index, array)
and needs to return apromise.
-
This will return a
promise
to be resolved containing the accumulator final value. -
Examples
- With
.then
-.catch
functions
const rgAsync = ;rgAsync// output => 6; // if exists any case that you throw an error on your reducer function- With
async/await
keywords
const rgAsync = ;// if you are inside of a async function scope// if exist any case that you throw an error you should wrap this with try-catch clausetryconst result = await rgAsync;console; // output => 6catcherrconsole; - With
Series
-
series(srcArray)
method invokes in series each item in the given source Array. -
This will return a
promise
to be resolved containing the same structure as thesrcArray
, but with the resolved values -
Example
const rgAsync = ;const list = async await // let assume that this will return a promise with resolved value of 1 async await // returns 2 as a resolved value async await // returns 3 as a resolved value Promise // returns 4 as a resolved value;// if you are inside of a async function scope// if exist a case that you throw an error you should wrap this with try-catch clausetry const result = await rgAsync; console; // output => [1,2,3,4]catcherr console; // if you aren't inside of async function scope you should use .then methodrgAsync ; // output => [1,2,3,4] ; // if exists a case that you throw an error on your list of promises
Parallel
-
parallel(srcArray)
method invokes in parallel each item in the given source Array. -
This will return a
promise
to be resolved containing the same structure as thesrcArray
, but with the resolved values -
Example
const rgAsync = ;const list = async await // let assume that this will return a promise with resolved value of 'one' async await // returns 'two' as a resolved value async await // returns 'three' as a resolved value Promise // returns 'four' as a resolved value;// if you are inside of a async function scope// if exist a case that you throw an error you should wrap this with try-catch clausetry const result = await rgAsync; console; // output => ['one','two','three','four']catcherr console; // if you aren't inside of async function scope you should use .then methodrgAsync ; // output => ['one','two','three','four'] ; // if exists a case that you throw an error on your list of promises