promisifyAll - Promisify a set of callback-based functions
parallel - Executes multiple PromiseResolver in parallel with given task limit
series - Executes multiple PromiseResolver in series
PromiseResolver - Utility function to cache a promised function including arguments for resolving. Required for advanced, promised based, control flows
const _asyncMagic = require('async-magic');const _fsMagic = require('fs-magic');const PromiseResolver = _asyncMagic.PromiseResolver;(async function(){ // task list const tasks = []; // stat a large list of files tasks.push(PromiseResolver(_fsMagic.stat, 'file1.txt')); tasks.push(PromiseResolver(_fsMagic.stat, 'file2.txt')); ... tasks.push(PromiseResolver(_fsMagic.stat, 'fileN.txt')); // resolves the promise with predefined arguments
series
Description: Executes multiple PromiseResolver in series
In case failOnError is not set, the resultset will contain the error object thrown during execution. Otherwise the executor will abort directly if an error has been thrown.
const _asyncMagic = require('async-magic');const _fsMagic = require('fs-magic');const PromiseResolver = _asyncMagic.PromiseResolver;(async function(){ // task list const tasks = []; // stat a large list of files tasks.push(PromiseResolver(_fsMagic.stat, 'file1.txt')); tasks.push(PromiseResolver(_fsMagic.stat, 'file2.txt')); ... tasks.push(PromiseResolver(_fsMagic.stat, 'fileN.txt')); // resolves the promise with predefined arguments)
PromiseResolver
Description: Utility function to cache a promised function including arguments for resolving. Required for advanced, promised based, control flows
const _asyncMagic = require('async-magic');const _fsMagic = require('fs-magic');const PromiseResolver = _asyncMagic.PromiseResolver;(async function(){ // caches the function with given arguments const task = PromiseResolver(_fsMagic.stat, 'file1.txt'); // resolves the promise with predefined arguments const stat = await task.resolve();})();
wait
Description: A Promise.all alias - waits until each promise has been completed or a single error occurs
Description: Intercept the current function exection for a given time asynchronous (does not stop the global event loop!)
Syntax:p:Promise = sleep(time:int)
const _asyncMagic = require('async-magic');(async function(){ console('Hello..'); // stop function execution for 1s (asynchronous!) await _asyncMagic.sleep(1000); console.log('World');})();
Mutex
Description: Mutex lock pattern for asynchronous operations
Syntax:p:Mutex = new Mutex()
const _asyncMagic = require('async-magic');(async function IOtask(){ // create Mutex const mutex = new _asyncMagic.Mutex(); // acquire lock await mutex.acquire(); // do something exclusively await directIoOperation(); // unlock mutex.release();})();
FAQ
What is the difference between named and anonymous functions ? (promisify)
Named functions contains its function-name as immutable .name attribute. This is especially useful during debugging, because the stacktrace will display the plain-text function name instead of "anonymous"
const _asyncMagic = require('../async-magic');const _fs = require('fs');// create a unnamed (standard) version and a named oneconst promisedStat = _asyncMagic.promisify(_fs.stat);const namedPromisedStat = _asyncMagic.promisify(_fs.stat, 'stat');// show function names (immutable .name attribute is set)console.log(promisedStat.name); //=> anonymousconsole.log(namedPromisedStat.name); //=> stat