zoukankan      html  css  js  c++  java
  • Promise A/+ 简单实现

    function Promise(exec){
      var self = this
      self.status = 'pending'//状态没用到。。。
      self.onResolvedList = []//当进行链式调用时,推入到列表
      self.onRejectedList = []//当进行链式调用时,推入到列表
      function resolved(reason){
        // if(self.status !== 'pending')return
        self.status = 'fulfilled'
        setTimeout(function(){//exec为同步函数时,将任务推到后面执行

          onResolvedList.forEach(function(fn){

            fn(reason)       

          })

        },0)
      }

      function rejected(reason){
        // if(self.status !== 'pending')return
        self.status = 'rejected'
        setTimeout(function(){//exec为同步函数时,将任务推到后面执行
          onRejectedList.forEach(function(fn){

            fn(reason)       

          })
        },0)
      }
      exec(resolved,rejected)//构造函数执行
    }

    Promise.prototype.then = function(resolved,rejected){
      var self = this
      self.onResolvedList.push(resolved)
      self.onRejectedList.push(rejected)
      return self
    }
    var P = new Promise(function(resolve,reject){
      // setTimeout(function(){
        resolve("resolve")
      // }, 1000)
    })
    P.then(function(value){
      console.log(value)
    }).then(function(value){
      setTimeout(function(){
        console.log(value)
      },1000)
    })

  • 相关阅读:
    SpringBoot定制修改Servlet容器
    springboot配置i18n
    idea properties编码问题
    多线程分页查询
    浏览器 私有属性&内核
    css添加样式的四种方式
    html、css、js注释
    JS中的 变量提升
    ECMAScript
    NOSCRIPT标签的用处
  • 原文地址:https://www.cnblogs.com/mooniitt/p/6774867.html
Copyright © 2011-2022 走看看