aa - async-await
co like library, go like channel, thunkify or promisify wrap package.
using ES6 (ES2015) generator function.
compatible with co@3 and co@4.
INSTALL:
$ npm install aa --save or$ npm install async-await --save
PREPARE:
var aa = ; // or var aa = ; var promisify = aapromisify; var thunkify = aathunkify; var promisifyAll = aapromisifyAll; var thunkifyAll = aathunkifyAll; var Channel = aaChannel; var Promise = aaPromise; // override native Promise var PromiseThunk = aaPromiseThunk; // use PromiseThunk indivisually
or
https://lightspeedworks.github.io/promise-thunk/promise-thunk.js
https://lightspeedworks.github.io/aa/aa.js
USAGE:
aa(generator or generator function)
aa()
returns promise (thunkified promise).
basic usage.
you can aa()
promises, generators, and generator functions.
;
aa.promisify([ctx,] fn, [options])
promisify()
converts node style function into a function returns promise-thunk.
you can use fs.exists()
and child_process.exec()
also.
ctx
: context object. default: this or undefined.fn
: node-style normal function.options
: options object.context
: context object.
also thenable, yieldable, callable.
pg
example:
postgres var pg = ;var pg_connect = aa; // -> yield pg_connect()var client_query = aa; // -> yield client_query()
aa.promisify(object, method, [options])
promisify()
defines method promisified function returns promise-thunk.
object
: target object.method
: method name string.options
: method name suffix or postfix. default: 'Async'. or options object.suffix
: method name suffix or postfix. default: 'Async'.postfix
: method name suffix or postfix. default: 'Async'.
pg
example:
postgres var pg = ;aa
aa.promisifyAll(object, [options])
promisifyAll()
defines all methods promisified function returns promise-thunk.
object
: target object.options
: method name suffix or postfix. default: 'Async'. or options object.suffix
: method name suffix or postfix. default: 'Async'.postfix
: method name suffix or postfix. default: 'Async'.
fs
example:
file system var fs = ;aa; // -> yield fs.readFileA()
pg
example:
postgres var pg = ;aa; // -> yield pg.connectA()aa; // -> yield client.connectAsync() // -> yield client.queryAsync()
aa.thunkify([ctx,] fn, [options])
thunkify()
converts node style function into a thunkified function.
you can use fs.exists()
and child_process.exec()
also.
ctx
: context object. default: this or undefined.fn
: node-style normal function with callback.options
: options object.context
: context object.
also yieldable, callable.
pg
example:
postgres var pg = ;var pg_connect = aa; // -> yield pg_connect()var client_query = aa; // -> yield client_query()
aa.thunkify(object, method, [options])
thunkify()
defines method thunkified function returns thunk.
object
: target object.method
: method name string.options
: method name suffix or postfix. default: 'Async'. or options object.suffix
: method name suffix or postfix. default: 'Async'.postfix
: method name suffix or postfix. default: 'Async'.
pg
example:
postgres var pg = ;aa; // -> yield pg.connectA()aa; // -> yield client.connectAsync()aa; // -> yield client.queryAsync()
aa.thunkifyAll(object, [options])
thunkifyAll()
defines all methods thunkified function returns thunk.
object
: target object.options
: method name suffix or postfix. default: 'Async'. or options object.suffix
: method name suffix or postfix. default: 'Async'.postfix
: method name suffix or postfix. default: 'Async'.
fs
example:
file system var fs = ;aa; // -> yield fs.readFileA()
pg
example:
postgres var pg = ;aa; // -> yield pg.connectA()aa; // -> yield client.connectAsync() // -> yield client.queryAsync()
aa.Channel() : new channel for event stream
Channel()
returns a new channel for event stream.
use a channel for node style function as a callback.
yield channel for wait it.
yield : waits and returns resolved value.
you can yield
promises, thunkified functions,
generators, generator functions,
primitive values, arrays, and objects.
aa.callback(gtor) : returns callback function
callback(gtor)
returns normal callback function
http;
EXAMPLES:
aa-readme-ex01-seq.js
Example 1 sequential:$ node aa-readme-ex01-seq.js
var aa = ; ; { console; console; console; ; ; } { console; console; console; } // asyncPromise(msec, arg) : promise { return { ; }; } // asyncThunk(msec, arg) : thunk { return { ; }; } // asyncGenerator(msec, arg) : generator { var chan = aa; ; return chan; }
aa-readme-ex02-par.js
Example 2 parallel:$ node aa-readme-ex02-par.js
var aa = ; ; { console; console; ; } { console; console; console; } // asyncPromise(msec, arg) : promise { return { ; }; } // asyncThunk(msec, arg) : thunk { return { ; }; } // asyncGenerator(msec, arg) : generator { var chan = aa; ; return chan; }
aa-readme-ex11-promisify.js
Example promisify:$ node aa-readme-ex11-promisify.js
var aa = ; var promisify = aapromisify; var asyncPromise = ; ; { console; console; console; ; } // asyncCallback(msec, arg. callback) : node style normal callback // callback : function (err, val) { ; }
aa-readme-ex12-thunkify.js
Example thunkify:$ node aa-readme-ex12-thunkify.js
var aa = ; var thunkify = aathunkify; var asyncThunk = ; ; { console; console; console; { console; { console; { console; }; }; }; } // asyncCallback(msec, arg. callback) : node style normal callback // callback : function (err, val) { ; }
aa-readme-example.js
Quick example collection:$ node aa-readme-example.js
var aa = ; // sleep(msec, args,... callback) : node style normal callback // callback : function (err, val) { var args = slice; setTimeout; } ; // delay(msec, args,...)(callback) : thunk // callback : function (err, val) { var args = slice; return { sleep; }; } // var delay = aa.thunkify(sleep); { console; } ; // aa.promisify(fn) : returns wrapped function a.k.a thunkify and promisify // wait(msec, args,...) : returns promise & thunk var wait = aa; // wait() : as a thunk { console; } ; // wait() : as a promise ; // aa(generator) : returns promise & thunk { console; return ; } { console; return ; } ;
LICENSE:
MIT