Async library for node.js
$ npm install tunasync
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.
It's used to add function when certain event is triggered
It's used to hinge a function on a certain event for a time specified by the third argument
name
String name of the eventfn
Function function which will be called when event is triggeredtimeout
Number time during which the function will process this event, and after which it will be removed from this event (optional, default0
)
It's used to add function which will occur only once
It's used to trigger events
name
String name of the eventargs
any arguments for functions
It`s used to detach (delete) a function from a specific event
Method to clear all events from emmiter or just one event
name
String name of event, optinal paramater
Return all listeners of event
name
String name of event
Returns Array of listeners
Return number of listeners of event, or number of events
name
String name of event, optinal paramater
Returns Number
Return array of all events of emitter
Returns Array
Applies the function fn to each argument and returns an array of values that the function returned.
fn
Function A function (with a callback or promise contract) that takes each argument as input and returns the processed value.fn.item
any current value.fn.itemInde
any index of the currently processed element in the array.fn.callback
any The callback function in which the processed value is passed, if fn function with callback contract.
arr
Array array of values. (optional, default[]
)config
Object object with settings for a function (optional, default{}
)
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
Function to memoize the function
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 constructor
Add a new task to the queue
Returns this
Set function that will be done after the all task of queue
fn
Function function to be done
Returns this
Completion of tasks in the queue and save their result
Returns Promise
Main function to export
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
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).
fn
Function A function (with a callback or promise contract) that takes each argument as input and returns the processed value.fn.acc
any accumulator.fn.item
any current value.fn.callback
Function The callback function in which the processed value is passed, if fn function with callback contract.
startValue
any your starting value, otherwise it's 0.arr
Array array of values. (optional, default[]
)config
Object object with settings for a function (optional, default{}
)
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 system fot async functions
fn
Function asynchronous function (with or without callback) that has to be repeated several times.args
Array array of arguments for the function. (optional, default[]
)config
Object object with settings for a function. (optional, default{}
)
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
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.
fns
Array array of functionsdone
Function final callbackconfig
(optional, default{}
)isCb
Boolean config for functions, is functions has callback logic or async
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 }
);
Applies the function fn to each argument and returns true if result of function with any arg is true
fn
Function A function (with a callback or promise contract) that takes each argument as input and returns the processed value.fn.item
any current value.fn.callback
Function The callback function in which the processed value is passed, if fn function with callback contract.
args
(optional, default[]
)config
Object object with settings for a function (optional, default{}
)arr
Array array of values.
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
Tun async is MIT licensed.