Skip to content

Instantly share code, notes, and snippets.

@susisu
Created August 23, 2013 13:12
Show Gist options
  • Save susisu/6319204 to your computer and use it in GitHub Desktop.
Save susisu/6319204 to your computer and use it in GitHub Desktop.
シーケンシャル処理的な
var fs = require("fs");
function seq()
{
var env = new Object();
for(var i = 1; i < arguments.length; i++)
{
arguments[i - 1].nextAction = arguments[i];
}
arguments[0].start(env);
}
function Action(func)
{
this.func = func;
this.nextAction = null;
}
Object.defineProperty(Action.prototype, "start", {
value: function(env)
{
this.func.call(env);
}
});
Object.defineProperty(Action.prototype, "next", {
value: function(env)
{
if(this.nextAction)
{
this.nextAction.start(env);
}
}
});
function _do(func)
{
var action = new Action(function()
{
func.call(this);
action.next(this);
});
return action;
}
function loadFile(name, path)
{
var action = new Action(function()
{
var env = this;
fs.readFile(path, function(err, file)
{
if(err)
{
console.log(err);
}
else
{
env[name] = file;
action.next(env);
}
});
});
return action;
}
function main()
{
seq(
_do(function()
{
console.log("hello");
}),
loadFile("foo", "foo.txt"),
loadFile("bar", "bar.txt"),
_do(function()
{
console.log(this.foo.toString() + this.bar.toString());
})
);
}
main();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment