Skip to content

Instantly share code, notes, and snippets.

@airtheva
Last active July 12, 2016 19:30
Show Gist options
  • Save airtheva/11212940 to your computer and use it in GitHub Desktop.
Save airtheva/11212940 to your computer and use it in GitHub Desktop.
// An example of using yield to simplify the callback hell.
// The limitation is, this only apply to whose callbacks take the normal form of function(err, result), more arguments are ignored.
// Maybe there are many workarounds, but keep it simple, the ECMAScript 6 is around the corner.
// Issues:
// 1. If you yield your syncTask(callback), it will complain that "Generator is already running."
// An solution is yield setTimeout(callback, 0) in your syncTask(callback).
function Async(task) {
var gen = task(callback);
function callback(err, result) {
if(err) {
throw err;
}
else {
gen.next(result);
}
};
gen.next();
};
Async(function*(callback) {
console.log(0);
// setTimeout's callback require no arguments, !err = true so that the gen.next() will be called.
yield setTimeout(callback, 1000);
console.log(1000);
yield setTimeout(callback, 1000);
console.log(2000);
yield setTimeout(callback, 1000);
console.log(3000);
return;
});
// ----------------------------------------------------------------
// Ex and Parallel.
function AsyncEx(task) {
var async = {};
var gen = task(async);
async.times = 0;
async.currentTimes = 0;
async.err = null;
async.results = [];
async.parallel = function(times) {
async.times = times;
async.currentTimes = 0;
async.err = null;
async.results = [];
};
async.timer = function(result) {
if(async.times > 0) {
async.results.push(result);
async.currentTimes++;
if(async.currentTimes == async.times) {
// Dirty reset.
async.times = 0;
return async.callbackErrResult(async.err, async.results);
}
else {
return;
}
}
};
async.endParallel = function() {
};
async.callbackResult = function(result) {
if(async.times > 0) {
async.timer(result);
}
else {
gen.next(result);
}
};
async.callbackErrResult = function(err, result) {
if(async.times > 0) {
async.timer(result);
}
else {
if(err) {
throw err;
}
else {
gen.next(result);
}
}
};
gen.next();
};
AsyncEx(function*(async) {
async.parallel(3);
console.log(0);
setTimeout(async.callbackResult, 1000);
console.log(1000);
setTimeout(async.callbackResult, 1000);
console.log(2000);
setTimeout(async.callbackResult, 1000);
console.log(3000);
var results = yield async.endParallel();
console.log(results);
console.log(1);
yield setTimeout(async.callbackResult, 1000);
console.log(1000);
yield setTimeout(async.callbackResult, 1000);
console.log(2000);
yield setTimeout(async.callbackResult, 1000);
console.log(3000);
async.parallel(3);
console.log(2);
setTimeout(async.callbackResult, 1000);
console.log(1000);
setTimeout(async.callbackResult, 1000);
console.log(2000);
setTimeout(async.callbackResult, 1000);
console.log(3000);
var results = yield async.endParallel();
console.log(results);
return;
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment