-
Notifications
You must be signed in to change notification settings - Fork 0
/
my-promise.html
95 lines (83 loc) · 1.99 KB
/
my-promise.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width,initial-scale=1,user-scalable=0">
<meta name="renderer" content="webkit">
<title>Promise A </title>
<meta name="description" content="首页描述"/>
<meta name="keywords" content="首页关键词"/>
<meta name="author" content="Web Layout:amu"/>
<meta name="format-detection" content="telephone=no,date=no,address=no,email=no,url=no"/>
<link rel="stylesheet" href="./src/style/base.css?v=1">
</head>
<body>
<script>
class MyPromise{
constructor(fn){
this.pState = 'pedding'
this.pResult = undefined
const resolve = (val)=>{
if(this.pState !== 'pedding') return;
this.pState = 'ok'
this.pResult = val
}
const reject = (er)=>{
if(this.pState !== 'pedding') return;
this.pState = 'fail'
this.pResult = er;
}
fn(resolve,reject)
}
then(onResolve,onReject){
//console.log(resolve)
//return resolve
return this;
}
catch(reject){
}
}
const nativePromise = new Promise((resolve,reject)=>{
resolve('成功')
})
console.log('原生',nativePromise)
let getValue = new MyPromise((resolve,reject)=>{
setTimeout(()=>{
resolve('结果2')
},5)
console.log(3)
})
console.log(1)
getValue.then((data,error)=>{
console.log(data)
console.log(error)
console.log(2)
})
getValue.catch((res)=>{
console.log(res)
})
console.log(getValue)
function Person(name){
this.name = name
}
Person.prototype.say = function(){
console.log(this.name)
}
let lili = new Person('lili')
lili.say()
function Animal(name){
this.name = name
}
Animal.prototype = new Person()
let maomi = {
name:'maomi'
}
lili.say.call(maomi)
lili.say.apply(maomi)
lili.say.bind(maomi)()
let dog = new Animal('dog')
dog.say()
</script>
</body>
</html>