/*
* pending:初始化成功
* fulfilled:成功
* rejected:失败
* */ function Promise(executor) {// 执行器
this.status = 'pending';
this.value = undefined;
this.reason = undefined;
this.fulfilledCallback = [];
this.rejectCallback = [];
let resolve = (value)=>{
if(this.status=='pending'){
this.status = 'resolve';
this.value = value;
this.fulfilledCallback.forEach(fn=>fn())
}
};
let reject = (reason)=>{
if(this.status =='pending'){
this.status = 'reject';
this.reason = reason;
this.rejectCallback.forEach(fn=>fn())
}
};
try{
executor(resolve,reject)
}catch(e){
reject(e)
}
}
Promise.prototype.then = function (onfulfilled,onrejected) {
if(this.status == 'resolve'){
onfulfilled(this.value)
}
if(this.status == 'reject'){
onrejected(this.reason)
}
if(this.status == 'pending'){
this.fulfilledCallback.push(()=>{
onfulfilled(this.value)
});
this.rejectCallback.push(()=>{
onrejected(this.reason)
})
}
}; var a = new Promise((resolve,reject)=>{
setTimeout(()=>{
resolve(10)
})
});
a.then((res)=>{
console.log(res);
});

js手写'Promise'的更多相关文章

  1. js 手写 Promise

    /* * pending:初始化成功 * fulfilled:成功 * rejected:失败 * */ function Promise(cback){ this.status = 'pending ...

  2. 常见的JS手写函数汇总(代码注释、持续更新)

    最近在复习面试中常见的JS手写函数,顺便进行代码注释和总结,方便自己回顾也加深记,内容也会陆陆续续进行补充和改善. 一.手写深拷贝 <script> const obj1 = { name ...

  3. 手写Promise A+ 规范

    基于ES6语法手写promise A+ 规范,源码实现 class Promise { constructor(excutorCallBack) { this.status = 'pending'; ...

  4. 原生html、js手写 radio与checkbox 美化

    原生html.js手写 radio与checkbox   美化 html <!DOCTYPE html> <html> <head> <meta charse ...

  5. 手写Promise中then方法返回的结果或者规律

    1. Promise中then()方法返回来的结果或者规律 我们知道 promise 的 then 方法返回来的结果值[result]是由: 它指定的回调函数的结果决定的 2.比如说下面这一段代码 l ...

  6. 五四青年节,今天要学习。汇总5道难度不高但可能遇到的JS手写编程题

    壹 ❀ 引 时间一晃,今天已是五一假期最后一天了,没有出门,没有太多惊喜与意外.今天五四青年节,脑子里突然想起鲁迅先生以及悲欢并不相通的话,我的五一经历了什么呢,忍不住想说那大概是,父母教育孩子大声嚷 ...

  7. 手写promise

    写在前面: 在目前的前端分开中,我们对于异步方法的使用越来越频繁,那么如果处理异步方法的返回结果,如果优雅的进行异步处理对于一个合格的前端开发者而言就显得尤为重要,其中在面试中被问道最多的就是对Pro ...

  8. 手写Promise看着一篇就足够了

    目录 概要 博客思路 API的特性与手写源码 构造函数 then catch Promise.resolved Promise.rejected Promise.all Promise.race 概要 ...

  9. 前端面试题之手写promise

    前端面试题之Promise问题 前言 在我们日常开发中会遇到很多异步的情况,比如涉及到 网络请求(ajax,axios等),定时器这些,对于这些异步操作我们如果需要拿到他们操作后的结果,就需要使用到回 ...

随机推荐

  1. ILRuntime官方Demo笔记

    调用/执行 热更中的方法 调用热更代码中方法,写在AppDomain中,记录一下主要几个方法: AppDomain.LoadAssembly 加载热更dll 执行热更代码的方法,有两种方式: appd ...

  2. 百度地图引用时 报出A Parser-blocking, cross site (i.e. different eTLD+1) script

    页面引入百度地图api时 chrome控制台报出警示问题 A Parser-blocking, cross site (i.e. different eTLD+1) script, http://ap ...

  3. MongoDB-BSON

    概念参考百科说明:BSON( Binary Serialized Document Format) 是一种二进制形式的存储格式,采用了类似于 C 语言结构体的名称.对表示方法,支持内嵌的文档对象和数组 ...

  4. cordova插件汇总

    1.获取当前应用的版本号 cordova plugin add cordova-plugin-app-version 2.获取网络连接信息 cordova plugin add cordova-plu ...

  5. 使用Harbor配置Kubernetes私有镜像仓库

    通常情况下,在私有云环境中使用kubernetes时,我们要从docker registry拉取镜像的时候,都会给docker daemo配置–insecure-registry属性来告诉docker ...

  6. [转帖]""(双引号) 与''(单引号) 差在哪?

    ""(双引号) 与''(单引号) 差在哪? http://wiki.jikexueyuan.com/project/13-questions-of-shell/double-sin ...

  7. mysql union 与 union all 语法及用法

    1.mysql   union  语法 mysql   union 用于把来自多个select  语句的结果组合到一个结果集合中.语法为: select  column,......from tabl ...

  8. python监控机器(第1版)

    # coding:utf-8 import configparser import logging import os import psutil import ctypes import platf ...

  9. Transformer

    参考资料: [ERT大火却不懂Transformer?读这一篇就够了] https://zhuanlan.zhihu.com/p/54356280 (中文版) http://jalammar.gith ...

  10. VisualStudio神级插件Resharper技巧基础入门到骨灰玩家使用全教程+Resharper性能优化

    原文地址:https://www.masuit.com/21/resharper 破解地址:https://www.masuit.com/20/resharper 官方文档:https://www.j ...