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)
    })

  • 相关阅读:
    抓包工具 Fiddler 使用介绍
    HTTP 协议常见首部字段
    HTTP 协议服务器相关概念
    HTTP 协议常见的状态码
    HTTP 协议中 GET 和 POST 方法详解
    设置html title标题左侧的小图标
    HTML页面如何判断是手机访问还是电脑访问
    使用Java的Frame类编写的QQ登录界面
    swing中JTable的使用方法
    采用MVC模式JDBC演示案例
  • 原文地址:https://www.cnblogs.com/mooniitt/p/6774867.html
Copyright © 2011-2022 走看看