1. const LazyMan = function(name){
    2. const array = []
    3. const fn = () => { console.log("Hi! This is "+name+ '!'); next() }
    4. const next = ()=>{
    5. const fn = array.shift()
    6. fn && fn()
    7. }
    8. array.push(fn)
    9. setTimeout(()=>{next()},0)
    10. const api = {
    11. sleep: (number) => {
    12. array.push(()=> {
    13. setTimeout(()=>{console.log('Wake up after '+ number); next() }, number*1000)
    14. })
    15. return api
    16. },
    17. eat: (content) => {
    18. array.push(() => {
    19. console.log('eat ' + content); next()
    20. })
    21. return api
    22. },
    23. sleepFirst: (number) => {
    24. array.unshift(() => {
    25. setTimeout(()=>{console.log('Wake up after '+5); next() }, number*1000)
    26. })
    27. return api
    28. }
    29. }
    30. return api
    31. }