Guest User

Untitled

a guest
Apr 26th, 2018
60
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.97 KB | None | 0 0
  1. // Class create
  2. class Lazyman {
  3. constructor(name) {
  4. this.task = [];
  5. const fn = () => {
  6. console.log(`Hi! This is ${name}`);
  7. this.next();
  8. }
  9. this.task.push(fn);
  10. setTimeout(() => {
  11. this.next();
  12. }, 0);
  13. }
  14.  
  15. next() {
  16. const fn = this.task.shift();
  17. fn && fn();
  18. }
  19.  
  20. sleepFirst(time) {
  21. const fn = () => {
  22. setTimeout(() => {
  23. console.log(`Wake up after ${time}`);
  24. this.next();
  25. }, time * 1000);
  26. }
  27. this.task.unshift(fn);
  28. return this;
  29. }
  30.  
  31. sleep(time) {
  32. const fn = () => {
  33. setTimeout(() => {
  34. console.log(`Wake up after ${time}`);
  35. this.next();
  36. }, time * 1000);
  37. }
  38. this.task.push(fn);
  39. return this;
  40. }
  41.  
  42. eat(type) {
  43. const fn = () => {
  44. console.log(`Eat ${type}~`);
  45. this.next()
  46. }
  47. this.task.push(fn);
  48. return this;
  49. }
  50. }
  51.  
  52. // Run Lazyman
  53. const lazyman = (name) => new Lazyman(name);
  54.  
  55.  
  56. // Example
  57. lazyman('hank').eat('dinner').sleep('3');
Add Comment
Please, Sign In to add comment