1.0.0 • Published 5 years ago
asyncforjs v1.0.0
AsyncFor
This library is designed to implement a sequential loop with the ability to use async functions within loop functions.
Examples
const AsyncFor = require('asyncforjs');
const sleep = ms => new Promise(resolve => setTimeout(resolve, ms));
async function main() {
let loop = new AsyncFor([1, 2, 3, 4]);
let array = [];
await loop.execute(async (value, key, dataset) => {
await sleep(value * 1000);
array.push({ key, value });
});
// After 10 seconds (because of sleep function in loop)
console.log(array); // [{ key: 0, value: 1 }, { key: 1, value: 2 }, { key: 2, value: 3 }, { key: 3, value: 4 }]
}
main();
Docs
new AsyncFor(Elements)
Constructor of AsyncFor
Param | Type | Description |
---|---|---|
Elements | Array | Dataset array |
AsyncFor.push(Elements)
Pushing new elements to loop data storage
Kind: instance method of AsyncFor
Param | Type | Description |
---|---|---|
Elements | Array | New dataset array |
AsyncFor.reset()
This function is resetting data storage
Kind: instance method of AsyncFor
AsyncFor.execute(func, another)
This function is loop dataset
Kind: instance method of AsyncFor
Param | Type | Default | Description |
---|---|---|---|
loop | Promise | Loop function | |
another | Boolean | false | Private variable |
Loop function(value, key, array)
Param | Type | Description |
---|---|---|
value | Any | Loop value |
key | Number | Loop key |
array | Array | Loop dataset |
1.0.0
5 years ago