• 博客园logo
  • 会员
  • 众包
  • 新闻
  • 博问
  • 闪存
  • 赞助商
  • HarmonyOS
  • Chat2DB
    • 搜索
      所有博客
    • 搜索
      当前博客
  • 写随笔 我的博客 短消息 简洁模式
    用户头像
    我的博客 我的园子 账号设置 会员中心 简洁模式 ... 退出登录
    注册 登录
LilyLiya
博客园    首页    新随笔    联系   管理    订阅  订阅
callStack and callback
call Stack------> callback----->callback Hell--------->Promise
  • what is call stack?
    • The mechanism the JS interpreter uses to keep track of its place in a script that calls multiple functions.
    • How js knows what functin is currently being run and what functions are called from within that function.
  • How does call stack work?
    • When a script calls a function, the interpreter adds it to the call stack and then starts carrying out the function
    • any functions that are called by that function are added to the call stack further up, and run where their calls are reached
    • when the current function is finished, the interpreter takes it off the stack and resumes execution where it left off the last code listing.
    • key ided: last in, first out
    • const multiply = (x, y) => x * y;
      
      const square = x => multiply(x, x);
      
      const isRightTriangle = (a, b, c) => (
          square(a) + square(b) === square(c)
      )
      console.log("BEFORE")
      isRightTriangle(3, 4, 5)
      
      console.log("DONEEEE!")

      square(a) -> multiply(a,a) ,要想执行square,得先执行multiply,multiply执行完毕之后从栈顶pop出去,然后再执行square(a)。

  • WHILE JS is SINGLE THREADED.单线程
    • At any given point in time, that single JS thread is running at most one line of JS code.
  • What happens when something takes time?
    • we have workaround
    • console.log("Sending request to server!")
      setTimeout(() => {
          console.log("Here is your data from the server...")
      }, 3000)
      console.log("I AM AT THE END OF THE FILE!")

      >Sending request to server!
      >I AM AT THE END OF THE FILE!
      >Here is your data from the server...

       

    • Browsera come with WEB APIs that are able to handle certain tasks in the background(like make=ing requests or setTimeout)
    • The JS call stack recognizes these Web API functions and passes them off to the browser to take care of
    • Once the browser finishes those tasks, they return and are pushed onto the stack as a callback
  • call back hell: 不停的嵌套setTimeOut
  • ===============
    YIKES!!!!!!!!!!!
    ===============
    setTimeout(() => {
        document.body.style.backgroundColor = 'red';
        setTimeout(() => {
            document.body.style.backgroundColor = 'orange';
            setTimeout(() => {
                document.body.style.backgroundColor = 'yellow';
                setTimeout(() => {
                    document.body.style.backgroundColor = 'green';
                    setTimeout(() => {
                        document.body.style.backgroundColor = 'blue';
                    }, 1000)
                }, 1000)
            }, 1000)
        }, 1000)
    }, 1000)

    代码又丑又麻烦!!!于是,就有了promise这个东西

posted on 2021-01-26 13:15  LilyLiya  阅读(93)  评论(0)    收藏  举报
刷新页面返回顶部
博客园  ©  2004-2025
浙公网安备 33010602011771号 浙ICP备2021040463号-3