Research
Security News
Quasar RAT Disguised as an npm Package for Detecting Vulnerabilities in Ethereum Smart Contracts
Socket researchers uncover a malicious npm package posing as a tool for detecting vulnerabilities in Etherium smart contracts.
async-deco
Advanced tools
A collection of decorators you can use to wrap functions (callback or promise based) to make them more robust
This is a collection of function decorators. It allows to timeout, retry, throttle, limit and much more! They can be combined together using the "compose" function (included).
All decorators are designed to work with functions using a callback or returning a promise. In case of callbacks, it must follow the node convention: the callback should be the last argument and its arguments should be, an error instance and the output of the function.
Every decorator is available in two different flavours:
var logDecorator = require('async-deco/callback/log');
This should be applied to functions with the node callback convention:
logDecorator(logger)(function (a, b, c, next) {
...
next(undefined, result); // or next(error);
});
var logDecorator = require('async-deco/promise/log');
This should be used for function returning promises:
logDecorator(logger)(function (a, b, c) {
return new Promise(function (resolve, reject) {
...
resolve(result); // or reject(error);
});
});
You can either:
var memoizeDecorator = require('async-deco/callback/memoize');
or
var memoizeDecorator = require('async-deco').callback.memoize;
or
var callbackDecorators = require('async-deco');
var memoizeDecorator = callbackDecorators.callback.memoize;
I strongly advice to use the first method, especially when using browserify. It allows to import only the functions you are actually using.
You can pass a logger to the decorators. This function is called with the same arguments of the original function and should return a function with this signature:
function (type, obj)
So for example, assuming that the first argument of the decorated function is a name:
var logger = function (name) {
return function (type, obj) {
console.log(name + ': ' + type);
};
};
The examples are related to the callback version. Just import the promise version in case of decorating promise based functions.
It allows to remember the last results
var memoizeDecorator = require('async-deco/callback/memoize');
var simpleMemoize = memoizeDecorator(getKey, logger);
simpleMemoize(function (..., cb) { .... });
It takes 2 arguments:
It is a more sophisticated version of the memoize decorator. It can be used to for caching in a db/file etc (You may have to write your own cache object). memoize-cache is an in-memory reference implementation (https://github.com/sithmel/memoize-cache).
var cacheDecorator = require('async-deco/callback/cache');
var cached = cacheDecorator(cache, logger);
cached(function (..., cb) { .... });
It takes 2 arguments:
If a function fails, calls another one
var fallbackDecorator = require('async-deco/callback/fallback');
var fallback = fallbackDecorator(function (err, a, b, c, func) {
func(undefined, 'giving up');
}, Error, logger);
fallback(function (..., cb) { .... });
It takes 3 arguments:
If a function fails, returns a value
var fallbackValueDecorator = require('async-deco/callback/fallback-value');
var fallback = fallbackValueDecorator('giving up', Error, logger);
fallback(function (..., cb) { .... });
It takes 3 arguments:
If a function fails, it tries to use a previous cached result
var fallbackCacheDecorator = require('async-deco/callback/fallback-cache');
var fallback = fallbackCacheDecorator(cache, Error, logger);
fallback(function (..., cb) { .... });
It takes 3 arguments:
Logs when a function start, end and fail
ar logDecorator = require('async-deco/callback/log');
var addLogs = logDecorator(logger);
addLogs(function (..., cb) { .... });
If a function takes to much, returns a timeout exception
var timeoutDecorator = require('async-deco/callback/timeout');
var timeout20 = timeoutDecorator(20, logger);
timeout20(function (..., cb) { .... });
This will wait 20 ms before returning a TimeoutError. It takes 2 arguments:
If a function fails, it retry running it again
var retryDecorator = require('async-deco/callback/retry');
var retryTenTimes = retryDecorator(10, 0, Error, logger);
retryTenTimes(function (..., cb) { .... });
You can initialise the decorator with 3 arguments:
Limit the parallel execution of a function.
var limitDecorator = require('async-deco/callback/limit');
var limitToTwo = limitDecorator(2, logger);
limitToTwo(function (..., cb) { .... });
You can initialise the decorator with 2 arguments:
It throttles or debounces the execution of a function. The callbacks returns normally with the result. Internally it uses the "getKey" function to group the callbacks into queues. It then executes the debounced (or throttled) function. When it returns a value it will run all the callbacks of the same queue.
var debounceDecorator = require('async-deco/callback/debounce');
var debounce = debounceDecorator(100, 'debounce', undefined, undefined, getLogger);
debounce(function (..., cb) { .... });
The arguments:
The options change meaning if they are related to the "throttle" or the "debounce":
For the debounce:
For the throttle:
For better understanding of throttle/debounce I suggest to read the "lodash" documentation and this article: https://css-tricks.com/the-difference-between-throttling-and-debouncing/.
Convert a synchronous/promise based function to a plain callback.
var callbackify = require('async-deco/utils/callbackify');
var func = callbackify(function (a, b){
return a + b;
});
func(4, 6, function (err, result){
... // result === 10 here
})
Convert a callback based function to a function returning a promise. (It uses https://www.npmjs.com/package/es6-promisify)
var promisify = require('async-deco/utils/promisify');
var func = promisify(function (a, b, next){
return next(undefined, a + b);
});
func(4, 6).then(function (result){
... // result === 10 here
})
It can combine more than one decorators. You can pass either an array or using multiple arguments. "undefined" functions are ignored.
var compose = require('async-deco/utils/compose');
var decorator = compose(
retryDecorator(10, Error, logger),
timeoutDecorator(20, logger));
decorator(function (..., cb) { .... });
Timeout after 20 ms and then retry 10 times before giving up. You should consider the last function is the one happen first!
FAQs
A collection of decorators for adding features to asynchronous functions
The npm package async-deco receives a total of 3 weekly downloads. As such, async-deco popularity was classified as not popular.
We found that async-deco demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Research
Security News
Socket researchers uncover a malicious npm package posing as a tool for detecting vulnerabilities in Etherium smart contracts.
Security News
Research
A supply chain attack on Rspack's npm packages injected cryptomining malware, potentially impacting thousands of developers.
Research
Security News
Socket researchers discovered a malware campaign on npm delivering the Skuld infostealer via typosquatted packages, exposing sensitive data.