This time I have researched aa which was introduced in Qiita (Japanese website).
aa is another library like co and utilize Generator mechanizm to make it easier to wirite parallel ans serial flows.
aa has some advantages to co, that is thunkify and promisify are included in the same package, and it has thunkifyAll and promisifyAll which enable affecting all the objects.
However, aa doesn't have a function equivalent to co.wrap, so it is a little inconvenient to write a yieldable function which uses parallel and serial flows.
const aa = require('aa');
var callbackObject = {
func: function(message, timeout, callback) {
setTimeout(function() {
console.log(message);
callback(null, message);
}, timeout);
}
};
var promisifiedObject = aa.promisifyAll(callbackObject);
aa(function*() {
console.log('--- aa start ---');
var response = yield [
promisifiedObject.funcAsync('aa two', 800),
promisifiedObject.funcAsync('aa one', 600),
];
console.log("response: " + JSON.stringify(response));
yield promisifiedObject.funcAsync('aa three', 200);
yield promisifiedObject.funcAsync('aa four', 400);
yield wrappedFunction();
console.log('--- aa end ---');
return "Generator finished.";
yield Promise.reject("An error happened.");
}).then(function(value){
console.log('--- then start ---');
console.log(value);
}).catch((error) => {
console.log('--- error start ---');
console.log(error);
});
var wrappedFunction = function() {
return aa(function* () {
console.log('--- wrap start ---');
var response = yield [
promisifiedObject.funcAsync('wrap two', 800),
promisifiedObject.funcAsync('wrap one', 600),
];
console.log("wrap response: " + JSON.stringify(response));
yield promisifiedObject.funcAsync('wrap three', 200);
yield promisifiedObject.funcAsync('wrap four', 400);
console.log('--- wrap end ---');
return yield Promise.resolve(null);
});
};
Result
$ node aa_generator.js
--- aa start ---
aa one
aa two
response: ["aa two","aa one"]
aa three
aa four
--- wrap start ---
wrap one
wrap two
wrap response: ["wrap two","wrap one"]
wrap three
wrap four
--- wrap end ---
--- aa end ---
--- then start ---
Generator finished.
Reference