Skip to content

Commit 2091124

Browse files
committed
small
1 parent 8d6ddad commit 2091124

File tree

17 files changed

+339
-1
lines changed

17 files changed

+339
-1
lines changed

_ecma/async-await/1.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
2.txt

_ecma/async-await/2.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
3.txt

_ecma/async-await/3.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
结束
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
let fs = require('fs')
2+
function read(file) {
3+
return new Promise(function(resolve, reject) {
4+
fs.readFile(file, 'utf8', function(err, data) {
5+
if (err) reject(err)
6+
resolve(data)
7+
})
8+
})
9+
}
10+
11+
async function read1() {
12+
let r = await read('1.txt','utf8')
13+
console.log(r)
14+
}
15+
async function read2() {
16+
let r = await read('2.txt','utf8')
17+
console.log(r)
18+
}
19+
20+
function readAll() {
21+
read1()
22+
read2() //这个函数同步执行
23+
}
24+
25+
readAll()
26+
// 2.txt 3.txt

_ecma/my-promise/myPromise.js

Lines changed: 214 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,214 @@
1+
// @from https://juejin.im/post/5b83cb5ae51d4538cc3ec354
2+
3+
// 判断变量否为function
4+
const isFunction = variable => typeof variable === 'function'
5+
6+
// 定义Promise的三种状态常量
7+
const PENDING = 'PENDING'
8+
const FULFILLED = 'FULFILLED'
9+
const REJECTED = 'REJECTED'
10+
11+
class MyPromise {
12+
13+
constructor (handle) {
14+
if (!isFunction(handle)) {
15+
throw new Error('MyPromise must accept a function as a parameter')
16+
}
17+
18+
// 添加状态
19+
this._status = PENDING
20+
// 添加状态
21+
this._value = undefined
22+
// 添加成功回调函数队列
23+
this._fulfilledQueues = []
24+
// 添加失败回调函数队列
25+
this._rejectedQueues = []
26+
27+
// 执行handle
28+
try {
29+
handle(this._resolve.bind(this), this._reject.bind(this))
30+
} catch (err) {
31+
this._reject(err)
32+
}
33+
}
34+
35+
// 添加resovle时执行的函数
36+
_resolve (val) {
37+
const run = () => {
38+
if (this._status !== PENDING) return
39+
// 依次执行成功队列中的函数,并清空队列
40+
const runFulfilled = (value) => {
41+
let cb;
42+
while (cb = this._fulfilledQueues.shift()) {
43+
cb(value)
44+
}
45+
}
46+
// 依次执行失败队列中的函数,并清空队列
47+
const runRejected = (error) => {
48+
let cb;
49+
while (cb = this._rejectedQueues.shift()) {
50+
cb(error)
51+
}
52+
}
53+
/* 如果resolve的参数为Promise对象,则必须等待该Promise对象状态改变后,
54+
当前Promsie的状态才会改变,且状态取决于参数Promsie对象的状态
55+
*/
56+
if (val instanceof MyPromise) {
57+
val.then(value => {
58+
this._value = value
59+
this._status = FULFILLED
60+
runFulfilled(value)
61+
}, err => {
62+
this._value = err
63+
this._status = REJECTED
64+
runRejected(err)
65+
})
66+
} else {
67+
this._value = val
68+
this._status = FULFILLED
69+
runFulfilled(val)
70+
}
71+
}
72+
// 为了支持同步的Promise,这里采用异步调用
73+
setTimeout(run, 0)
74+
}
75+
76+
// 添加reject时执行的函数
77+
_reject (err) {
78+
if (this._status !== PENDING) return
79+
// 依次执行失败队列中的函数,并清空队列
80+
const run = () => {
81+
this._status = REJECTED
82+
this._value = err
83+
let cb;
84+
while (cb = this._rejectedQueues.shift()) {
85+
cb(err)
86+
}
87+
}
88+
// 为了支持同步的Promise,这里采用异步调用
89+
setTimeout(run, 0)
90+
}
91+
92+
// 添加then方法
93+
then (onFulfilled, onRejected) {
94+
const { _value, _status } = this
95+
// 返回一个新的Promise对象
96+
return new MyPromise((onFulfilledNext, onRejectedNext) => {
97+
// 封装一个成功时执行的函数
98+
let fulfilled = value => {
99+
try {
100+
if (!isFunction(onFulfilled)) {
101+
onFulfilledNext(value)
102+
} else {
103+
let res = onFulfilled(value);
104+
if (res instanceof MyPromise) {
105+
// 如果当前回调函数返回MyPromise对象,必须等待其状态改变后在执行下一个回调
106+
res.then(onFulfilledNext, onRejectedNext)
107+
} else {
108+
//否则会将返回结果直接作为参数,传入下一个then的回调函数,并立即执行下一个then的回调函数
109+
onFulfilledNext(res)
110+
}
111+
}
112+
} catch (err) {
113+
// 如果函数执行出错,新的Promise对象的状态为失败
114+
onRejectedNext(err)
115+
}
116+
}
117+
// 封装一个失败时执行的函数
118+
let rejected = error => {
119+
try {
120+
if (!isFunction(onRejected)) {
121+
onRejectedNext(error)
122+
} else {
123+
let res = onRejected(error);
124+
if (res instanceof MyPromise) {
125+
// 如果当前回调函数返回MyPromise对象,必须等待其状态改变后在执行下一个回调
126+
res.then(onFulfilledNext, onRejectedNext)
127+
} else {
128+
//否则会将返回结果直接作为参数,传入下一个then的回调函数,并立即执行下一个then的回调函数
129+
onFulfilledNext(res)
130+
}
131+
}
132+
} catch (err) {
133+
// 如果函数执行出错,新的Promise对象的状态为失败
134+
onRejectedNext(err)
135+
}
136+
}
137+
switch (_status) {
138+
// 当状态为pending时,将then方法回调函数加入执行队列等待执行
139+
case PENDING:
140+
this._fulfilledQueues.push(fulfilled)
141+
this._rejectedQueues.push(rejected)
142+
break
143+
// 当状态已经改变时,立即执行对应的回调函数
144+
case FULFILLED:
145+
fulfilled(_value)
146+
break
147+
case REJECTED:
148+
rejected(_value)
149+
break
150+
}
151+
})
152+
}
153+
154+
// 添加catch方法
155+
catch (onRejected) {
156+
return this.then(undefined, onRejected)
157+
}
158+
159+
// 添加静态resolve方法
160+
static resolve (value) {
161+
// 如果参数是MyPromise实例,直接返回这个实例
162+
if (value instanceof MyPromise) return value
163+
return new MyPromise(resolve => resolve(value))
164+
}
165+
166+
// 添加静态reject方法
167+
static reject (value) {
168+
return new MyPromise((resolve ,reject) => reject(value))
169+
}
170+
171+
// 添加静态all方法
172+
static all (list) {
173+
return new MyPromise((resolve, reject) => {
174+
/**
175+
* 返回值的集合
176+
*/
177+
let values = []
178+
let count = 0
179+
for (let [i, p] of list.entries()) {
180+
// 数组参数如果不是MyPromise实例,先调用MyPromise.resolve
181+
this.resolve(p).then(res => {
182+
values[i] = res
183+
count++
184+
// 所有状态都变成fulfilled时返回的MyPromise状态就变成fulfilled
185+
if (count === list.length) resolve(values)
186+
}, err => {
187+
// 有一个被rejected时返回的MyPromise状态就变成rejected
188+
reject(err)
189+
})
190+
}
191+
})
192+
}
193+
194+
// 添加静态race方法
195+
static race (list) {
196+
return new MyPromise((resolve, reject) => {
197+
for (let p of list) {
198+
// 只要有一个实例率先改变状态,新的MyPromise的状态就跟着改变
199+
this.resolve(p).then(res => {
200+
resolve(res)
201+
}, err => {
202+
reject(err)
203+
})
204+
}
205+
})
206+
}
207+
208+
finally (cb) {
209+
return this.then(
210+
value => MyPromise.resolve(cb()).then(() => value),
211+
reason => MyPromise.resolve(cb()).then(() => { throw reason })
212+
);
213+
}
214+
}

_ecma/my-promise/myPromise.test.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
// 测试

_ecma/promise/.gitkeep

Whitespace-only changes.

_ecma/yield/1.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
2.txt

_ecma/yield/2.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
3.txt

_ecma/yield/3.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
结束

0 commit comments

Comments
 (0)