async-magic
Promises FTW! A pure promised based, straight forward async library for Node.js >=7.6.
yarn add async-magic --save
Features
- Convert callback based functions into promised based once (with named functions)
- Advanced promised based control flows (parallel, series)
- Run a set of promised functions in parallel with given number of maximum parallel tasks
- Queue promised based function including arguments
- Standalone, no external dependencies required
- Designed to run with the pure power of native
Promise
,await
andasync function
- No backward compatibility layer
API
- promisify - Promisify a callback-based function
- 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
- wait - Promise.all alias
- sleep - Intercept the current function execution
- Mutex - asynchronous Mutex lock pattern
promisify
Description: Promisify a callback-based function
Requirements:
- Last function argument has to be the callback (Nodejs Standard)
- The first argument of the callback has to be a possible error (Nodejs Standard)
Syntax: fn:function = promisify(fn:function, [functionName:String])
Arguments:
- fn:function - the callback based function which should be converted into a promise based
- functionName:String(optinal) - an optional name which is used as native js function name
Example:
const _asyncMagic = ;const _fs = ; // create promisified fs-stat. set "stat" as native function nameconst fsStat = _asyncMagic; // use promisified version{ const fstats = await ; console;};
promisifyAll
Description: Promisify a set of callback-based functions
Syntax: promisifiedFunctionSet:Object = promisifyAll(functionSet:Object, functionNames:StringArray)
Arguments:
- functionSet:Object - a set of functions identified by the objects keys
- functionNames:StringArray - a list of the object keys which should be promisified (names are also taken as native function names!)
Example:
Promisify some fs functions. Just as showcase, take a look at fs-magic - it is based on async-magic.
const _asyncMagic = ;const _fs = ; // list of fs methods to promisifyconst fsApi = 'access' 'appendFile' 'chmod'; // create promisified fs versionconst _fsPromised = _asyncMagic; // use promisified version{ const fstats = await _fsPromised; console;};
parallel
Description: Executes multiple PromiseResolver
in parallel with given task limit
Syntax: results:array = parallel(resolvers:array, [limit:int=1000])
const _asyncMagic = ;const _fsMagic = ;const PromiseResolver = _asyncMagicPromiseResolver; { // task list const tasks = ; // stat a large list of files tasks; tasks; ... tasks; // resolves the promise with predefined arguments // limit the number of parallel executed promises to 100 (IO handle limitation) const stats = await _asyncMagic;};
series
Description: Executes multiple PromiseResolver
in series
Syntax: results:array = series(resolvers:array, [failOnError:boolean=true])
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 = ;const _fsMagic = ;const PromiseResolver = _asyncMagicPromiseResolver; { // task list const tasks = ; // stat a large list of files tasks; tasks; ... tasks; // resolves the promise with predefined arguments) const stats = await _asyncMagic;};
PromiseResolver
Description: Utility function to cache a promised function including arguments for resolving. Required for advanced, promised based, control flows
Syntax: p:PromiseResolver = PromiseResolver(fn:function, [...args:any])
const _asyncMagic = ;const _fsMagic = ;const PromiseResolver = _asyncMagicPromiseResolver; { // caches the function with given arguments const task = ; // resolves the promise with predefined arguments const stat = await task;};
wait
Description: A Promise.all alias - waits until each promise has been completed or a single error occurs
Syntax: p:Promise = wait(promises:Array)
const _asyncMagic = ;const _fsMagic = ; // stat multiple files at once{ const stats = await _asyncMagic;};
sleep
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 = ; { console'Hello..'; // stop function execution for 1s (asynchronous!) await _asyncMagic; console;};
Mutex
Description: Mutex lock pattern for asynchronous operations
Syntax: p:Mutex = new Mutex()
const _asyncMagic = ; { // create Mutex const mutex = ; // acquire lock await mutex; // do something exclusively await ; // unlock mutex;};
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 = ;const _fs = ; // create a unnamed (standard) version and a named oneconst promisedStat = _asyncMagic;const namedPromisedStat = _asyncMagic; // show function names (immutable .name attribute is set)console; //=> anonymousconsole; //=> stat
Any Questions ? Report a Bug ? Enhancements ?
Please open a new issue on GitHub
License
async-magic is OpenSource and licensed under the Terms of The MIT License