aa - async-await library.
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 = require('aa');
var aa = require('async-await');
USAGE:
aa(generator or generator function) : returns promise (thunkified promise)
basic usage.
you can aa()
promises, generators, and generator functions.
aa(function *() {
yiled 1;
yiled [1, 2, 3];
yiled {x:1, y:2, z:3};
yield Promise.resolve(1);
try { yield Promise.reject(new Error('expected')); }
catch (e) { console.error('%s', e); }
yield [Promise.resolve(1), Promise.resolve(2)];
yield {x: Promise.resolve(1), y: Promise.resolve(2)};
});
aa.promisify(node style function) : function returns promise
promisify()
converts node style function into a function returns promise.
you can use fs.exists()
and child_process.exec()
also.
aa.thunkify(node style function) : function returns thunk
thunkify()
converts node style function into a thunkified function.
you can use fs.exists()
and child_process.exec()
also.
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.
EXAMPLES:
$ node aa-readme-ex01-seq.js
var aa = require('aa');
aa(main);
function *main() {
console.log('11:', yield asyncPromise(100, 11));
console.log('12:', yield asyncThunk(100, 12));
console.log('13:', yield asyncGenerator(100, 13));
yield sub(20);
yield sub(30);
}
function *sub(base) {
console.log('%s: %s', base + 1, yield asyncPromise(100, base + 1));
console.log('%s: %s', base + 2, yield asyncThunk(100, base + 2));
console.log('%s: %s', base + 3, yield asyncGenerator(100, base + 3));
}
function asyncPromise(msec, arg) {
return new Promise(function (resolve, reject) {
setTimeout(resolve, msec, arg);
});
}
function asyncThunk(msec, arg) {
return function (callback) {
setTimeout(callback, msec, null, arg);
};
}
function *asyncGenerator(msec, arg) {
var chan = aa.Channel();
setTimeout(chan, msec, arg);
return yield chan;
}
$ node aa-readme-ex02-par.js
var aa = require('aa');
aa(main);
function *main() {
console.log('[11, 12, 13]:', yield [
asyncPromise(100, 11),
asyncThunk(100, 12),
asyncGenerator(100, 13)
]);
console.log('{x:11, y:12, z:13}:', yield {
x: asyncPromise(100, 11),
y: asyncThunk(100, 12),
z: asyncGenerator(100, 13)
});
yield [sub(20), sub(30)];
}
function *sub(base) {
console.log('%s: %s', base + 1, yield asyncPromise(100, base + 1));
console.log('%s: %s', base + 2, yield asyncThunk(100, base + 2));
console.log('%s: %s', base + 3, yield asyncGenerator(100, base + 3));
}
function asyncPromise(msec, arg) {
return new Promise(function (resolve, reject) {
setTimeout(resolve, msec, arg);
});
}
function asyncThunk(msec, arg) {
return function (callback) {
setTimeout(callback, msec, null, arg);
};
}
function *asyncGenerator(msec, arg) {
var chan = aa.Channel();
setTimeout(chan, msec, arg);
return yield chan;
}
$ node aa-readme-ex11-promisify.js
var aa = require('aa');
var promisify = aa.promisify;
var asyncPromise = promisify(asyncCallback);
aa(main);
function *main() {
console.log('11:', yield asyncPromise(100, 11));
console.log('12:', yield asyncPromise(100, 12));
console.log('13:', yield asyncPromise(100, 13));
asyncPromise(100, 21)
.then(function (val) {
console.log('21:', val);
return asyncPromise(100, 22);
})
.then(function (val) {
console.log('22:', val);
return asyncPromise(100, 23);
})
.then(function (val) {
console.log('23:', val);
});
}
function asyncCallback(msec, arg, callback) {
setTimeout(callback, msec, null, arg);
}
$ node aa-readme-ex12-thunkify.js
var aa = require('aa');
var thunkify = aa.thunkify;
var asyncThunk = thunkify(asyncCallback);
aa(main);
function *main() {
console.log('11:', yield asyncThunk(100, 11));
console.log('12:', yield asyncThunk(100, 12));
console.log('13:', yield asyncThunk(100, 13));
asyncThunk(100, 21)
(function (err, val) {
console.log('21:', val);
asyncThunk(100, 22)
(function (err, val) {
console.log('22:', val);
asyncThunk(100, 23)
(function (err, val) {
console.log('23:', val);
});
});
});
}
function asyncCallback(msec, arg, callback) {
setTimeout(callback, msec, null, arg);
}
$ node aa-readme-example.js
var aa = require('aa');
function sleep(msec) {
var args = [].slice.call(arguments, 1);
setTimeout.apply(null, [args.pop(), msec, null].concat(args));
}
sleep(1000, function (err, val) { console.log('1000 msec OK'); });
function delay(msec) {
var args = [].slice.call(arguments);
return function (callback) {
sleep.apply(null, args.concat(callback));
};
}
delay(1100)(
function (err, val) { console.log('1100 msec OK'); }
);
var wait = aa.promisify(sleep);
wait(1200)(
function (err, val) { console.log('1200 msec OK'); }
);
wait(1300).then(
function (val) { console.log('1300 msec OK'); },
function (err) { console.log('1300 msec NG', err); }
).catch(
function (err) { console.log('1300 msec NG2', err); }
);
aa(function *() {
yield 1;
yield [1, 2, 3];
yield {x:1, y:2, z:3};
yield Promise.resolve(2);
yield delay(800);
yield wait(800);
console.log('0:', yield wait(300, 0));
console.log('1:', yield wait(300, 1));
console.log('[1, 2, 3]:',
yield Promise.all([wait(200, 1), wait(300, 2), wait(100, 3)]));
console.log('[4, 5, 6]:',
yield [wait(200, 4), wait(300, 5), wait(100, 6)]);
console.log('{x:7, y:8, z:9}:',
yield {x:wait(200, 7), y:wait(300, 8), z:wait(100, 9)});
var chan = aa.Channel();
sleep(300, 20, chan);
sleep(200, 10, chan);
var a = yield chan;
var b = yield chan;
console.log('10 20:', a, b);
aa(function *() {
yield wait(200);
return 200;
})(chan);
aa(function *() {
yield wait(100);
return 100;
})(chan);
aa(function *() {
yield wait(300);
return 300;
})(chan);
var x = yield chan;
var y = yield chan;
var z = yield chan;
console.log('top 3 winners: 100 200 300:', x, y, z);
var chan1 = aa.Channel(), chan2 = aa.Channel();
aa(function *() {
sleep(100, 111, chan1);
console.log('222:', yield chan2);
sleep(100, 333, chan1);
console.log('444:', yield chan2);
sleep(100, 555, chan1);
return 666;
})(chan);
aa(function *() {
console.log('111:', yield chan1);
sleep(100, 222, chan2);
console.log('333:', yield chan1);
sleep(100, 444, chan2);
console.log('555:', yield chan1);
return 777;
})(chan);
console.log('666 777:', yield chan, yield chan);
return 11;
})
.then(
function (val) {
console.log('11 val:', val);
return wait(100, 22); },
function (err) {
console.log('11 err:', err);
return wait(100, 22); }
)
(function (err, val) {
console.log('22 val:', val, err ? 'err:' + err : '');
return wait(100, 33); })
(function (err, val) {
console.log('33 val:', val, err ? 'err:' + err : '');
return wait(100, 44); })
.then(
function (val) {
console.log('44 val:', val);
return wait(100, 55); },
function (err) {
console.log('44 err:', err);
return wait(100, 55); }
)
.catch(
function (err) {
console.log('55 err:', err);
return wait(100, 66); }
);
LICENSE:
MIT