Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Class create
- class Lazyman {
- constructor(name) {
- this.task = [];
- const fn = () => {
- console.log(`Hi! This is ${name}`);
- this.next();
- }
- this.task.push(fn);
- setTimeout(() => {
- this.next();
- }, 0);
- }
- next() {
- const fn = this.task.shift();
- fn && fn();
- }
- sleepFirst(time) {
- const fn = () => {
- setTimeout(() => {
- console.log(`Wake up after ${time}`);
- this.next();
- }, time * 1000);
- }
- this.task.unshift(fn);
- return this;
- }
- sleep(time) {
- const fn = () => {
- setTimeout(() => {
- console.log(`Wake up after ${time}`);
- this.next();
- }, time * 1000);
- }
- this.task.push(fn);
- return this;
- }
- eat(type) {
- const fn = () => {
- console.log(`Eat ${type}~`);
- this.next()
- }
- this.task.push(fn);
- return this;
- }
- }
- // Run Lazyman
- const lazyman = (name) => new Lazyman(name);
- // Example
- lazyman('hank').eat('dinner').sleep('3');
Add Comment
Please, Sign In to add comment