Some additional functions to advance the capabilities of asynchronous code in javascript.
Ever wanted to do more with javascript async. If you simply use async/await, all your doing is making a function sync itself, but not affecting other functions.
Why not instead, start an async process, and await the result later in the function when you actually need it.
That's what this module does, it simplifies that process, and adds some additional functions that may be useful.
This module uses no dependencies, and does not rely on nodejs. It is capable of running in the browser (might add in a future update).
What about for loops? Why not make an async version of .map()? Check the rest of this readme file, this function was added to the module.
The .map() and .forEach() methods of this module also support objects in the loop (auto running Object.keys()). Strings, numbers, booleans, etc. will also be converted to be in an array.
Installation
npm install @aspiesoft/async-extra
Setup
const asyncExtra = require('@aspiesoft/async-extra');
Usage
async function myFunction(){
let options = {
timeout: 3000,
checkInterval: 10,
};
let operation = asyncExtra.function(options, myLongProcess1, myLongProcess2, myLongProcess3);
await operation.onFinish((result, index, results, finished) => {
});
let result = await operation.result();
console.log(result);
result = operation.resultSync();
console.log(result);
if(result[0]){
}
await operation.wait();
operation.setOptions({timeout: 3000, checkInterval: 5});
operation = asyncExtra.function({resultObject: true});
result = await operation.firstResult();
result = await operation.resultIndex(index);
operation.addIndex(index, callback);
}
await asyncExtra.forEach(objectOrArray, callback);
await asyncExtra.map(objectOrArray, callback, {timeout: 5000, checkInterval: 5});
await asyncExtra.sleep(ms);
let value = false;
await asyncExtra.waitForValue(() => value, timeout, checkInterval);