webpack基础小结。
参考 Promise A+ 简单实现 promise,用的setTimeout 模拟的异步,与实际浏览器Promise 有出入(具体可以看Event loop 相关),本文只做思路理解参考。
Promise 的简单实现,主要理解规范,也可以再理解一下几个点方变记忆:
- Promise的三种状态(pending,fulfilled,rejected), pending状态转移后不可变
- resolve , resolve a promise with a promise
- reject
- Promise.prototype.then 返回一个新的promise 对象(链式操作)
- thenable对象
1.简单实现
const PENDING = 'pending'
const FULFILLED = 'fulfilled'
const REJECTED = 'rejected' function needResolve() {
throw new TypeError('need pass function to Promise')
} function needuseNew() {
throw new TypeError(
'Promise is a constructor and should be called with the `new` keyword'
)
} function Promise(task) {
var self = this
this.state = PENDING
this.value
this.fulArr = []
this.rejArr = [] function resolve(value) {
if (value != null && value.then && typeof value.then == 'function') {
return value.then(resolve, reject)
}
setTimeout(() => {
if (self.state == PENDING) {
self.state = FULFILLED
self.value = value
self.fulArr.forEach(item => {
item(self.value)
})
}
})
} function reject(reason) {
setTimeout(() => {
if (self.state == PENDING) {
self.state = REJECTED
self.value = reason
self.fulArr.forEach(item => {
item(self.value)
})
}
})
}
typeof task !== 'function' && needResolve()
!(this instanceof Promise) && needResolve()
try {
task(
function(value) {
resolve(value)
},
function(reason) {
reject(reason)
}
)
} catch (e) {
reject(e)
}
} function resolvePromise(promise, x, resolve, reject) {
if (promise === x) {
return reject(new TypeError('do not resolve promise with itself'))
}
var then, called
// resolve promise with promise
if (x instanceof Promise) {
if (x.status == PENDING) {
x.then(function(y) {
resolvePromise(promise, y, resolve, reject)
}, reject)
} else {
x.then(resolve, reject)
}
// thenable 其它的then
} else if (x !== null && (typeof x == 'function' || typeof x == 'object')) {
try {
// resolve promise with promise
then = x.then
if (typeof then === 'function') {
then.call(
x,
function(y) {
if (called) {
return
}
called = true
resolvePromise(promise, y, resolve, reject)
},
function(r) {
if (called) {
return
}
called = true
reject(r)
}
)
} else {
resolve(x)
}
} catch (e) {
if (called) {
return
}
called = true
reject(e)
}
} else {
resolve(x)
}
}
Promise.prototype.then = function(onFulfilled, onRejected) {
var self = this
onFulfilled =
typeof onFulfilled === 'function'
? onFulfilled
: function(value) {
return value
}
onRejected =
typeof onRejected === 'function'
? onRejected
: function(reason) {
throw reason
}
var promise2
if (self.state == PENDING) {
promise2 = new Promise(function(resolve, reject) {
self.fulArr.push(value => {
try {
let x = onFulfilled(value)
resolvePromise(promise2, x, resolve, reject)
} catch (e) {
reject(e)
}
})
self.reject.push(reason => {
try {
let x = onRejected(reason)
resolvePromise(promise2, x, resolve, reject)
} catch (e) {
reject(e)
}
})
})
}
if (self.state == FULFILLED) {
promise2 = new Promise(function(resolve, reject) {
setTimeout(() => {
try {
let x = onFulfilled(self.value)
resolvePromise(promise2, x, resolve, reject)
} catch (e) {
reject(e)
}
})
})
}
if (self.state == REJECTED) {
promise2 = new Promise(function(resolve, reject) {
setTimeout(() => {
try {
let x = onRejected(self.value)
resolvePromise(promise2, x, resolve, reject)
} catch (e) {
reject(e)
}
})
})
}
// then 必须返回一个新的promise
return promise2
} Promise.prototype.catch = function(onRejected) {
return this.then(null, onRejected)
} Promise.resolve = function(value) {
return new Promise((resolve, reject) => {
resolve(value)
})
}
Promise.reject = function(value) {
return new Promise((resolve, reject) => {
reject(value)
})
} function gen(time, resolve) {
let result = []
let i = 0
return function(index, value) {
if (i < time) {
i++
result[index] = value
} else {
resolve(result)
}
}
} Promise.race = function(arr) {
return new Promise((resolve, reject) => {
for (let i = 0; i < arr.length; i++) {
Promise.resolve(arr[i]).then(resolve, reject)
}
})
}
Promise.all = function(arr) {
return new Promise((resolve, reject) => {
let done = gen(arr.length, resolve)
for (let i = 0; i < arr.length; i++) {
Promise.resolve(arr[i]).then(function(value) {
done(value, i)
}, reject)
}
})
}
module.exports = Promise
webpack基础小结。的更多相关文章
- js学习笔记:webpack基础入门(一)
之前听说过webpack,今天想正式的接触一下,先跟着webpack的官方用户指南走: 在这里有: 如何安装webpack 如何使用webpack 如何使用loader 如何使用webpack的开发者 ...
- webpack基础+webpack配置文件常用配置项介绍+webpack-dev-server
一.webpack基础 1.在项目中生成package.json:在项目根目录中输入npm init,根据提示输入相应信息.(也可以不生成package.json文件,但是package.json是很 ...
- 搭建webpack基础配置
搭建webpack基础步骤: 1.去官方网站下载node.js(根据自己电脑的系统类型选择) 2.安装node.js完成后打开cmd命令提示符: 出现版本号证明安装成功 3.cd到工程目录下 npm ...
- Java 基础--小结
Java 基础--小结 java基础 Java源程序(.java文件)——>java字节码文件(.class文件)——>由解释执行器(java.exe)将字节码文件加载到java虚拟机( ...
- nodejs+gulp+webpack基础知识
nodejs+gulp+webpack基础知识 2019年08月22日 11:49:40 天府云创 阅读数 22 版权声明:本文为博主原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文 ...
- Webpack基础学习
Webpack基础学习:https://segmentfault.com/a/1190000008853009
- 学习webpack基础笔记01
学习webpack基础笔记 1.webpack搭建环境最重要的就是如何使用loader和plugins,使用yarn/npm安装插件.预处理器,正确的配置好去使用 2.从0配置webpack - 1. ...
- android基础小结
(注:此小结文档在全屏模式下观看效果最佳) 2016年3月1日,正式开始了我的android学习之路. 最最开始的,当然是学习怎样搭载环境了,然而苦逼的我在win10各种坑爹的指引下还是安装了一个星期 ...
- 【webpack】---模块打包机webpack基础使用---【巷子】
001.什么是webpack? 作用有哪些? WebPack可以看做是模块打包机:它做的事情是,分析你的项目结构,找到JavaScript模块以及其它的一些浏览器不能直接运行的拓展语言(Scss,Ty ...
随机推荐
- 配置maven项目的开发时的默认jdk版本
配置所有maven项目的默认jdk版本,若不配置则提示"Warning:java: 源值1.5已过时, 将在未来所有发行版中删除" 在settings.xml文件中profiles ...
- Beta(0/7)
组长重选议题 Beta阶段计划改进完善的功能 Beta阶段计划新增的功能 需要改进的团队分工 需要改进的工具流程 Beta冲刺的时间计划安排 组长重选议题 没人想当,所以没有换. Beta阶段计划改进 ...
- 按比例缩放DIV
class ResponsiveDiv extends React.Component { constructor(props) { super(props); this.state = { widt ...
- 如何运行后台Service?
Unless you specify otherwise, most of the operations you do in an app run in the foreground on a spe ...
- [HDU4669]Editor (栈)
题意 模拟编辑器,还是给链接吧 https://vjudge.net/problem/HDU-4699 思路 两个栈 代码 //poj1050 //n^4暴力 #include<algorith ...
- [BZOJ2457][BeiJing2011]双端队列 (单调性)
正如lyd所说,和数据结构本身没什么太大关联 题意 中文题面 Sherry现在碰到了一个棘手的问题,有N个整数需要排序. Sherry手头能用的工具就是若干个双端队列. ...
- python#读csv,excel,json数据
#读csv,excel,json数据 with open('E:\\test\\xdd.csv','r') as f: for line in f.readlines(): print(line) i ...
- 使用gitflow流程完成功能时报错
报错 fatal: could not read Username for 'https://github.com': ······ 原因 使用https方式的时候 在https url 里面没有用户 ...
- Android总结之json解析(FastJson Gson 对比)[申明:来源于网络]
Android总结之json解析(FastJson Gson 对比)[申明:来源于网络] 地址:http://blog.csdn.net/u014031072/article/details/5392 ...
- 自学stm32就要记住入了这个“大坑”要耐得住寂寞
在现在的MCU使用量中,STM32绝对是翘楚!因为现在使用STM32开发产品的公司非常多,这主要得益于ST公司对自家MCU的大力推广,而且ST对自己MCU也配套了一系列开发软件,也有相应的硬件开发板供 ...