Git Product home page Git Product logo

Comments (3)

luckymore avatar luckymore commented on August 17, 2024 1

妙啊,next 执行器,贯穿始终

from fe-weekly-questions.

LuckyWinty avatar LuckyWinty commented on August 17, 2024
function LazyMan(name) {
    if(!(this instanceof LazyMan)){
        return new LazyMan(name)
    }
  const cb = (next)=>{
      console.log(`Hi This is ${name}!`)
      next()
  }
  this.cbs = [cb];
  setTimeout(()=>{
    this.next()
  },0)
}
LazyMan.prototype.eat = function (food){
    const cb = (next)=>{
        console.log(`Eat ${food}~`)
        next()
    } 
    this.cbs.push(cb);
    return this
}
LazyMan.prototype.sleepFirst = function (time){
    const cb = (next)=>{
        setTimeout(()=>{
            next()
        },time*1000) 
    } 
    this.cbs.unshift(cb);
    return this
}
LazyMan.prototype.sleep = function(time){
    const cb = (next)=>{
        setTimeout(()=>{
            next()
        },time*1000) 
    } 
    this.cbs.push(cb);
    return this
}
LazyMan.prototype.next = function(){
    if(this.cbs.length <= 0)return
    const first = this.cbs.shift()
    first(this.next.bind(this))
}

from fe-weekly-questions.

chenjigeng avatar chenjigeng commented on August 17, 2024
function LazyMan(str) {
  const tasks = [
    {
      type: "hello",
      msg: `Hi! This is ${str}!`,
    },
  ];

  const sleep = function (s) {
    tasks.push({
      type: "sleep",
      time: s,
    });
    return this;
  };

  const sleepFirst = function (s) {
    tasks.unshift({
      type: "sleep",
      time: s,
    });
    return this;
  };

  const eat = function (s) {
    tasks.push({
      type: "eat",
      food: s,
    });
    return this;
  };

  const next = () => {
    if (tasks.length === 0) return;
    const task = tasks.shift();

    if (task.type === "sleep") {
      setTimeout(() => {
        next();
      }, task.time * 1000);
    } else if (task.type === "hello") {
      console.log(task.msg);
      next();
    } else {
      console.log(`eat ${task.food}!`);
      next();
    }
  };

  setTimeout(() => {
    next();
  });

  return {
    sleep,
    sleepFirst,
    eat,
  };
}

LazyMan("Hank");
LazyMan("Hank").sleep(10).eat("dinner");
LazyMan("Hank").sleep(10).eat("dinner").eat("supper");
LazyMan("Hank").sleepFirst(5).eat("supper");

from fe-weekly-questions.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.