Created
November 25, 2011 13:58
-
-
Save KOBA789/1393588 to your computer and use it in GitHub Desktop.
real-async-without-indent
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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