1.0.0 • Published 5 years ago

asyncforjs v1.0.0

Weekly downloads
2
License
ISC
Repository
github
Last release
5 years ago

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

ParamTypeDescription
ElementsArrayDataset array

AsyncFor.push(Elements)

Pushing new elements to loop data storage

Kind: instance method of AsyncFor

ParamTypeDescription
ElementsArrayNew 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

ParamTypeDefaultDescription
loopPromiseLoop function
anotherBooleanfalsePrivate variable

Loop function(value, key, array)

ParamTypeDescription
valueAnyLoop value
keyNumberLoop key
arrayArrayLoop dataset