tunasync v1.0.0
Tun async

Async library for node.js
API
Table of Contents
AsyncEmitter
Now, an event emitter is an object/method which triggers an event as soon as some action takes place so as to pass the control to the parent function.
on
It's used to add function when certain event is triggered
Parameters
onTemporary
It's used to hinge a function on a certain event for a time specified by the third argument
Parameters
nameString name of the eventfnFunction function which will be called when event is triggeredtimeoutNumber time during which the function will process this event, and after which it will be removed from this event (optional, default0)
once
It's used to add function which will occur only once
Parameters
emit
It's used to trigger events
Parameters
nameString name of the eventargsany arguments for functions
remove
It`s used to detach (delete) a function from a specific event
Parameters
clear
Method to clear all events from emmiter or just one event
Parameters
nameString name of event, optinal paramater
listeners
Return all listeners of event
Parameters
nameString name of event
Returns Array of listeners
count
Return number of listeners of event, or number of events
Parameters
nameString name of event, optinal paramater
Returns Number
names
Return array of all events of emitter
Returns Array
map
Applies the function fn to each argument and returns an array of values that the function returned.
Parameters
fnFunction A function (with a callback or promise contract) that takes each argument as input and returns the processed value.fn.itemany current value.fn.itemIndeany index of the currently processed element in the array.fn.callbackany The callback function in which the processed value is passed, if fn function with callback contract.
arrArray array of values. (optional, default[])configObject object with settings for a function (optional, default{})
Examples
const arr = [1, 2, 3, 5];
map(
(item, cb) => {
setTimeout(() => {
cb(null, item + 2);
}, 10);
},
arr,
{ isCb: true }
)
.then(data => console.log(data))
.catch(err => console.log(err.message));Returns Array an array of processed values or an error
asyncMemoize
Function to memoize the function
Parameters
Examples
const sum = async (a, b) => {
await sleep(100);
return a + b;
};
const memoizedSum = memoize(sum);
const result = [];
const expectedResult = [4, 6, 4];
memoizedSum(1, 3)
.then(res => result.push(res))
.then(() => memoizedSum(1, 5))
.then(res => result.push(res))
.then(() => memoizedSum(1, 3))
.then(res => result.push(res))
.them(() => console.log(result));Returns any the result of function from cache or calculated result
Queue
Queue constructor
pushTask
Add a new task to the queue
Parameters
Returns this
done
Set function that will be done after the all task of queue
Parameters
fnFunction function to be done
Returns this
doTasks
Completion of tasks in the queue and save their result
Returns Promise
queue
Main function to export
Examples
const q = queue();
const createTask = v => new Promise(res => setTimeout(() => res(v), v));
q.pushTask(createTask, [100])
.pushTask(createTask, [200])
.pushTask(createTask, [300]);
q.done(result => console.log(result));
q.doTasks();Returns Queue new instance of Queue
reduce
The reduce() method reduces the array to a single value. The reduce() method executes a provided function for each value of the array (from left-to-right). The return value of the function is stored in an accumulator (result/total).
Parameters
fnFunction A function (with a callback or promise contract) that takes each argument as input and returns the processed value.fn.accany accumulator.fn.itemany current value.fn.callbackFunction The callback function in which the processed value is passed, if fn function with callback contract.
startValueany your starting value, otherwise it's 0.arrArray array of values. (optional, default[])configObject object with settings for a function (optional, default{})
Examples
reduce(
(acc, item, callback) => {
setTimeout(() => callback(null, acc + item), 1000);
},
10,
[1, 2, 3, 4, 5, 6],
{ isCb: true }
)
.then(res => console.log(res))
.catch(err => console.log(err.message));Returns any result of calculations
retry
Retry system fot async functions
Parameters
fnFunction asynchronous function (with or without callback) that has to be repeated several times.argsArray array of arguments for the function. (optional, default[])configObject object with settings for a function. (optional, default{})
Examples
const fnPromises = require('fs').promises;
retry(fsPromises.readFile, ['some.js', 'utf8'], {
retries: 5,
interval: 10,
}).then(data => console.log(data));Returns any the value of the fn, or an error
series
Running multiple functions which depend on the output of the previous function. If an error is encountered in any of the tasks, no more functions are run but the final callback is called with the error value.
Parameters
fnsArray array of functionsdoneFunction final callbackconfig(optional, default{})isCbBoolean config for functions, is functions has callback logic or async
Examples
const createFn = value => (err, cb) => {
setTimeout(() => {
if (value === 6) return cb(new Error('sorry'));
else return cb(err, value);
}, value);
};
const fns = [createFn(2000), createFn(60), createFn(500)];
series(
fns,
(err, data) => {
const result = { err, data };
console.log(result);
},
{ isCb: true }
);some
Applies the function fn to each argument and returns true if result of function with any arg is true
Parameters
fnFunction A function (with a callback or promise contract) that takes each argument as input and returns the processed value.fn.itemany current value.fn.callbackFunction The callback function in which the processed value is passed, if fn function with callback contract.
args(optional, default[])configObject object with settings for a function (optional, default{})arrArray array of values.
Examples
some(
(filePath, callback) => {
fs.access(filePath, err => {
callback(null, !err);
});
},
['file1', 'file2', 'retry.test.js'],
{ isCb: true }
)
.then(res => console.log(res))
.catch(err => console.log(err.message));Returns Boolean shows if function from any arg returns true
Contributors
License
Tun async is MIT licensed.
5 years ago