Last active
September 14, 2016 06:46
-
-
Save atwayne/08d1b176c20100806e3ed5a4ef799946 to your computer and use it in GitHub Desktop.
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
function LazyMan(name) { | |
return new _LazyMan(name); | |
} | |
function _LazyMan(name) { | |
var self = this; | |
self.name = name; | |
self.tasks = []; | |
var greeting = function() { | |
console.log("Hello this is " + self.name); | |
self.next(); | |
}; | |
self.tasks.push(greeting); | |
setTimeout(function() { | |
self.next(); | |
}, 0); | |
} | |
_LazyMan.prototype.next = function() { | |
var fn = this.tasks.shift(); | |
fn && fn(); | |
}; | |
_LazyMan.prototype.eat = function(eatWhat) { | |
var self = this; | |
var fn = (function(eatWhat) { | |
return function() { | |
console.log('Eat ' + eatWhat); | |
self.next(); | |
}; | |
})(eatWhat); | |
self.tasks.push(fn); | |
return self; | |
}; | |
_LazyMan.prototype.sleep = function(seconds) { | |
var self = this; | |
var fn = (function(seconds) { | |
setTimeout(function() { | |
self.next(); | |
}, seconds * 1000); | |
})(seconds); | |
self.tasks.push(fn); | |
return self; | |
}; | |
_LazyMan.prototype.sleepFirst = function(seconds) { | |
var self = this; | |
var fn = (function(seconds) { | |
setTimeout(function() { | |
self.next(); | |
}, seconds * 1000); | |
})(seconds); | |
self.tasks.unshift(fn); | |
return self; | |
}; | |
LazyMan('Hank') | |
.eat('lunch') | |
.sleep(1) | |
.eat('supper') | |
.sleepFirst(2); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment