Skip to content

Repository files navigation

Tun async

Testing CICodacy Badgelicense

Logo

Async library for node.js

$ npm install tunasync

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
  • nameString name of the event
  • fnFunction function which will be called when event is triggered

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 event
  • fnFunction function which will be called when event is triggered
  • timeoutNumber time during which the function will process this event, and after which it will be removed from this event (optional, default 0)

once

It's used to add function which will occur only once

Parameters
  • nameString name of the event
  • fnFunction function which will be called when event is triggered

emit

It's used to trigger events

Parameters
  • nameString name of the event
  • argsany arguments for functions

remove

It`s used to detach (delete) a function from a specific event

Parameters
  • nameString name of the event
  • fnFunction function that we want to remove from this event

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

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

constarr=[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

  • fnFunction function to memoize
  • configObject object with config (optional, default {})
    • config.isCbBoolean function accepts callback
    • config.cacheSizeNumber max size of cache

Examples

constsum=async(a,b)=>{awaitsleep(100);returna+b;};constmemoizedSum=memoize(sum);constresult=[];constexpectedResult=[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
  • fnFunction task to add to the queue
  • argsArray array of arguments for task (optional, default [])

Returns this

done

Set function that will be done after the all task of queue

Parameters

Returns this

doTasks

Completion of tasks in the queue and save their result

Returns Promise

queue

Main function to export

Examples

constq=queue();constcreateTask=v=>newPromise(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

constfnPromises=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 functions
  • doneFunction final callback
  • config (optional, default {})
  • isCbBoolean config for functions, is functions has callback logic or async

Examples

constcreateFn=value=>(err,cb)=>{setTimeout(()=>{if(value===6)returncb(newError('sorry'));elsereturncb(err,value);},value);};constfns=[createFn(2000),createFn(60),createFn(500)];series(fns,(err,data)=>{constresult={ 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.

Releases

Used by

Contributors

Languages