Skip to content

Instantly share code, notes, and snippets.

@KOBA789
Created November 25, 2011 13:58
Show Gist options
  • Select an option

  • Save KOBA789/1393588 to your computer and use it in GitHub Desktop.

Select an option

Save KOBA789/1393588 to your computer and use it in GitHub Desktop.
real-async-without-indent
var fs = require('fs');
function readFile (filename) {
return function () {
fs.readFile(filename, 'utf8', arguments.callee.next);
}
}
function getData() {
return function (err, data) {
if (err) throw err;
if (arguments.callee.next) {
arguments.callee.next(data);
}
}
}
function putStrLn () {
return function (data) {
console.log(data);
if (arguments.callee.next) {
arguments.callee.next();
}
}
}
function wait (time) {
return function () {
setTimeout(arguments.callee.next, time);
}
}
function param (val) {
return function () {
arguments.callee.next(val);
}
}
var DoAsync = new Function();
DoAsync.prototype.run = function () {
this.next();
}
DoAsync.prototype.__defineSetter__('do', function (val) {
var origin = this;
while (true) {
if ('next' in origin) {
origin = origin.next;
} else {
origin.next = val;
break;
}
}
return null;
});
var main = new DoAsync();
main.do = new param('hello');
main.do = new putStrLn;
main.do = new wait(1000);
main.do = new param('world');
main.do = new putStrLn;
main.run();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment