Async-Ray
Purpose of this package is to provide async/await
callbacks for every
, filter
, find
, findIndex
, forEach
, map
, reduce
, reduceRight
and some
methods in Array.
https://rpgeeganage.github.io/async-ray/doc/
TypeScript Doc:Content
Basic usage
const AsyncRay = ;
Supported methods
.aEvery
.aEvery(async callback(element[, index[, array]]))
{ return Promise;} const inputArray = 10 20 30 40; // Call Every methodconst output = await ; console;// Output is true
.aFilter
.aFilter(async callback(element[, index[, array]]))
{ return Promise;} const inputArray = 1 2 3 4; // Call Filter methodconst filterArray = await ; console;// Output is [3, 4]
.aFind
.aFind(async callback(element[, index[, array]]))
Find will return the found value or undefined
{ return Promise;} const inputArray = 1 2 3 4; // Call Find methodconst outputElement = await ; console;// Output is 2
.aFindIndex
.aFindIndex(async callback(element[, index[, array]]))
FindIndex will return the index of found value or -1
{ return Promise;} const inputArray = 1 2 3 4; // Call Find methodconst outputIndex = await ; console;// Output is 1
.aFlatMap
.aFlatMap(async callback(element[, index[, array]]))
{ return Promise;} const inputArray = 1 2 3 4; // Call Map methodconst flatMappedArray = await ;console;// Output is [1, 2, 2, 4, 3, 6, 4, 8]
.aForEach
.aForEach(async callback(element[, index[, array]]))
{ return Promise;} const inputArray = 1 2 3 4;const outputArray = ; // Call ForEach methodawait ; console;// Output is [1, 2, 3, 4]
.aMap
.aMap(async callback(element[, index[, array]]))
{ return Promise;} const inputArray = 1 2 3 4; // Call Map methodconst mappedArray = await ;console;// Output is [1, 2, 3, 4]
.aReduce
.aReduce(async callback(accumulator, element[, index[, array]]), [initialValue])
{ return Promise;} const inputArray = 10 20 30 40; // Call Reduce methodconst output = await ; console;// Output is 101
.aReduceRight
.aReduceRight(async callback(accumulator, element[, index[, array]]), [initialValue])
{ return Promise;} const inputArray = 10 20 30 40; // Call Reduce methodconst output = await ; console;// Output is 101
.aSome
.aSome(async callback(element[, index[, array]]))
{ return Promise;} const inputArray = 10 20 30 40; // Call Some methodconst output = await ; console;// Output is true
Using methods individually
You can use each method without creating AsyncRay
object.
; // aEveryconst everyResult = await ; // aFilterconst filterResult = await ; // aFindconst findResult = await ; // aFindIndexconst findIndexResult = await ; // aForEachconst forEachResult: number = ;await ; // aMapconst mapResult = await ; // aReduceconst reduceResult = await ; // aReduceRightconst reduceRightResult = await ; // aSomeconst someResult = await ;
Chaining
Chain
functionality
Async-Ray methods can be chained using Basic usage
const Chain = ;
Chaining will return an instance of Async-Ray if returned type is an array.
aMap()
and aFilter()
sample 1 - sample code)
(The process()
method must be called explicitly to process the chain because aMap()
and aFilter()
method returns an array.
const input = 1 2 3; const op = await // Call the process() method to execute the chain ; console;// Output is [ 200, 300 ]
aMap()
, aFilter()
and aFind()
sample 2 - sample code)
(The process()
method should not be called because aFind()
does not return an array.
const input = 1 2 3; const op = await ; // No need to call process() method console;// Output is 300
Array methods methods
Between otherChain
with filter()
Sample 1 - Async-Ray const input = 1 2 3; const op = await console;// Output is [ 300 ]
Chain
with find()
Sample 2 - Async-Ray const input = 1 2 3; const op = await console;// Output is 200