只考虑成功时的调用,方便理解一下promise的原理
promise的例子:

1、

接下来一步步实现一个简单的promise

step1:
promise 接受一个函数作为构造函数的参数,是立即执行的,并且返回个resolve函数作为该函数的回调

function CPromise(fun){
  function resolve(){
  }
fun(resolve);
}

step2:

promise的then方法接受一个函数作为参数,并且该函数的参数值,来自于resolve方法中传入的参数,说明resolve运行时,实际上运行了then函数中作为参数的函数(注册该函数)

调用then方法(相当于先注册返回成功后需要执行的函数,参考观察者模式),实际上就是在promise异步返回后,将所有需要处理的回调函数放入callbacks中,当resolve触发时,逐个执行callbacks中的回调。

function CPromise(fun){
this.callback = null;
  function resolve(data){
this.callback(data);
  }
this.then = function(callback){
this.callback = callback;
}
fun(resolve);
}

此时运行

step3:

说明在resolve方法被运行时,then中的函数还没有被注册(因为代码是同步的,所以直接就运行了resolve),所以加一个延迟,保证调用时一定被注册了,call用来绑定Cpromise 中的this作用域,原生promise并不需要

function CPromise(fun){
this.callback = null;
  function resolve(data){
    setTimeout(()=>{
      this.callback(data);     });
  }
this.then = function(callback){
this.callback = callback;
}
fun.call(this,resolve);//绑定this作用域,this.callback时使用
}

step4:

满足多次调用then,可以设置一个数组存储then中注册的函数

function CPromise(fun){
this.callbacks = [];
  function resolve(data){
    setTimeout(()=>{
      this.callbacks.forEach(function(fun){
  fun(data);
  });
   });
  }
this.then = function(callback){
this.callbacks.push(callback);
}
fun.call(this,resolve);
}

step5:
加入状态机制,当promise的状态还在Pending时,then中注册的事件就push到队列中,如果已经Fulfilled,那么之后注册的事件立即执行

promise的例子:

theh中的事件是在resolve运行后才注册的,说明当状态已经是Fulfilled后,所有注册的事件都是立即执行的

function CPromise(fun){
this.callbacks = [],
state='pending',
value = null;
  function resolve(data){
this.value = data;
    setTimeout(()=>{
this.state = 'fulfilled';
      this.callbacks.forEach(function(fun){
   fun(data);
   });
   });
  }
this.then = function(callback){
if(this.state == 'pending'){
this.callbacks.push(callback);
}
callback(this.value);
}
fun.call(this,resolve);
} function a(){
var c = b();
setTimeout(function(){
console.info();
c.then(function(data){
console.info(data);
})
c.then(function(data){
console.info('a'+data);
})
},);
}
function b(){
return new CPromise(function(resolve) {
console.info();
resolve.call(this,);
})
}
a();

实现简单的promise的更多相关文章

  1. 一个简单的Promise 实现

    用了这么长时间的promise,也看了很多关于promise 的文章博客,对promise 算是些了解.但是要更深的理解promise,最好的办法还是自己实现一个. 我大概清楚promise 是对异步 ...

  2. 如何用原生JS实现一个简单的promise

    我又又又回来了,最近真是累的跟狗一样,急需一个大保健回复一下子精力 我现在是一边喝着红牛一边写着博客,好了好了,不扯了,回归整体好吧 先简单来说一下啥是promise吧 它是什么?Promise是一个 ...

  3. 简单版 Promise/A+,通过官方872个测试用例

    promise 标准 在实现 Promise 之前要清楚的是 JavaScript 中的 Promise 遵循了 Promises/A+ 规范,所以我们在编写 Promise 时也应当遵循这个规范,建 ...

  4. 聊一聊看似简单的Promise.prototype.then()方法

    Promise.prototype.then() Proise实例的then方法是定义在原型对象Promise.prototype上的,它的作用是为Promise实例添加状态改变时的回调函数. 该方法 ...

  5. 简单模拟 Promise

    class promise { constructor(fn) { this.data = null; this.err = null; this.isPromise = false; this.er ...

  6. [手写系列] 带你实现一个简单的Promise

    简介 学习之前 需要先对Promise有个基本了解哦,这里都默认大家都是比较熟悉Promise的 本次将带小伙伴们实现Promise的基本功能 Promise的基本骨架 Promise的then Pr ...

  7. 简单的 Promise 实现

    参考 http://www.tuicool.com/articles/RzQRV3 var PENDING = undefined, FULLFILLED = 1, REJECTED = 2; var ...

  8. 简单的 Promise 实现 一

    const Promise = function(fn){ let state = { pending: "pending", fulfilled: "fulfilled ...

  9. 手写简单的promise

    function Promise(fn) { var that = this; this.status = "pedding"; this.value = undefined; / ...

随机推荐

  1. maven配置及IDEA配置maven环境

    一. maven的下载及配置 1. maven下载地址 可以在网址:https://maven.apache.org/download.cgi下载最新版本的maven 2. maven文件解压缩 解压 ...

  2. springboot 共享session

    1.pom添加jar依赖 <dependency> <groupId>org.springframework.boot</groupId> <artifact ...

  3. P5284 [十二省联考2019]字符串问题

    这是一道涵盖了字符串.图论.数据结构三个方面的综合大题. 把这道题放在D1T2的人应该拖出去打 前置芝士 首先,您至少要会topsort. 其次,如果您只想拿个暴力分,字符串Hash就足够了:如果您想 ...

  4. python时间模块小结

    1.datetime 模块 为日期和时间处理同时提供了简单和复杂的方法.支持日期和时间算法的同时,实现的重点放在更有效的处理和格式化输出.该模块还支持时区处理: 简单例子: from datetime ...

  5. stylus 实践

    音乐分享: Broken Back - <Halcyon Birds> —————————————————————————————————————————————————————————— ...

  6. LibreOJ一本通题解报告

    网页跳转 解析啥的以后会有的 目录 ·T1活动安排 ·T2种树 ·T3喷水装置 T1活动安排 /* problem:yibentong10000 date:2019/4/21 author:Lonel ...

  7. Xshell 连接虚拟机出现 "The remote SSH server rejected X11 forwarding request"

    1. 描述 虚拟机:VirtualBox Linux: centOS7 解决了 centOS7在VirtualBox中装好后的网络连接问题 后,用 Xshell 连接服务器时出现下面情况: 2. ss ...

  8. java程序设计第二次作业

  9. Zabbix(二)

    zabbix 监控第一台服务器 https://blog.51cto.com/5001660/2136303 一.搭建一台测试服务器 1.安装一台centos7操作系统 配置网络: vim /etc/ ...

  10. Linux 下 MySQL-5.6.16 安装

    转载请注明出处!!!! 卸载mysql 1 查找以前是否装有 mysql 命令:rpm -qa|grep -i mysql 2 将所有包删除 删除命令:rpm -e --nodeps  包名 3 删除 ...