1.0.0 • Published 3 years ago

tunasync v1.0.0

Weekly downloads
-
License
MIT
Repository
github
Last release
3 years ago

Tun async

Testing CI Codacy Badge license

Logo

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
  • name String name of the event
  • fn Function 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
  • name String name of the event
  • fn Function function which will be called when event is triggered
  • timeout Number 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
  • name String name of the event
  • fn Function function which will be called when event is triggered

emit

It's used to trigger events

Parameters
  • name String name of the event
  • args any arguments for functions

remove

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

Parameters
  • name String name of the event
  • fn Function function that we want to remove from this event

clear

Method to clear all events from emmiter or just one event

Parameters
  • name String 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
  • name String 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

  • 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 {})

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

  • fn Function function to memoize
  • config Object object with config (optional, default {})
    • config.isCb Boolean function accepts callback
    • config.cacheSize Number max size of cache

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
  • fn Function task to add to the queue
  • args Array 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

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

  • 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 {})

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

  • 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 {})

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

  • fns Array array of functions
  • done Function final callback
  • config (optional, default {})
  • isCb Boolean 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

  • 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.

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.