|   632(字)

Promise初探

上面的动画
用传统的回调的方法控制动画实现

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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Promise animation</title>
<style type="text/css" media="screen">
.ball{
width: 40px;
height: 40px;
border-radius: 20px;
}
.ball1{
background: red;
}
.ball2{
background: yellow;
}
.ball3{
background: green;
}
</style>
</head>
<body>
<div class="ball ball1" style="margin-left:0px;"></div>
<div class="ball ball2" style="margin-left:0px;"></div>
<div class="ball ball3" style="margin-left:0px;"></div>
<script type="text/javascript">
var ball1 = document.querySelector('.ball1')
var ball2 = document.querySelector('.ball2')
var ball3 = document.querySelector('.ball3')
function animate(ball,distance,callback){
setTimeout(function(){
var marginLeft = parseInt(ball.style.marginLeft,10)
if (marginLeft === distance) {
callback && callback()
}
else {
if (marginLeft < distance) {
marginLeft ++
}
else{
marginLeft --
}
ball.style.marginLeft = marginLeft+'px'
animate(ball, distance,callback)
}
},13)
}
animate(ball1,100,function(){
animate(ball2,200,function(){
animate(ball3,300,function(){
animate(ball3,150,function(){
animate(ball2,150,function(){
animate(ball1,150,function(){
})
})
})
})
})
})
</script>
</body>
</html>

这种方式存在 回调地狱

我们用promise来实现一下

promise的厉害之处就在于只用了一个api接口同时解决回调地狱和异步异常流程控制,还能轻松处理异步和同步流程混杂,说是JS语言内部缺少的一部分也不为过(实际上已经出现了原生Promise)

这里用bluebird这个库

1
npm install bluebird

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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Promise animation</title>
<style type="text/css" media="screen">
.ball{
width: 40px;
height: 40px;
border-radius: 20px;
}
.ball1{
background: red;
}
.ball2{
background: yellow;
}
.ball3{
background: green;
}
</style>
<script type="text/javascript" src="node_modules/bluebird/js/browser/bluebird.js"></script>
</head>
<body>
<div class="ball ball1" style="margin-left:0px;"></div>
<div class="ball ball2" style="margin-left:0px;"></div>
<div class="ball ball3" style="margin-left:0px;"></div>
<script type="text/javascript">
var ball1 = document.querySelector('.ball1')
var ball2 = document.querySelector('.ball2')
var ball3 = document.querySelector('.ball3')
function animate(ball,distance,callback){
setTimeout(function(){
var marginLeft = parseInt(ball.style.marginLeft,10)
if (marginLeft === distance) {
callback && callback()
}
else {
if (marginLeft < distance) {
marginLeft ++
}
else{
marginLeft --
}
ball.style.marginLeft = marginLeft+'px'
animate(ball, distance,callback)
}
},13)
}
// animate(ball1,100,function(){
// animate(ball2,200,function(){
// animate(ball3,300,function(){
// animate(ball3,150,function(){
// animate(ball2,150,function(){
// animate(ball1,150,function(){
// })
// })
// })
// })
// })
// })
var Promise =window.Promise
function promiseAnimate(ball,distance){
return new Promise(function(resolve,reject){
function _animate(){
setTimeout(function(){
var marginLeft = parseInt(ball.style.marginLeft,10)
if (marginLeft === distance) {
resolve()
}
else {
if (marginLeft < distance) {
marginLeft ++
}
else{
marginLeft --
}
ball.style.marginLeft = marginLeft+'px'
_animate()
}
},13)
}
_animate()
})
}
promiseAnimate(ball1,100)
.then(function(){
return promiseAnimate(ball2,200)
})
.then(function(){
return promiseAnimate(ball3,300)
})
.then(function(){
return promiseAnimate(ball3,150)
})
.then(function(){
return promiseAnimate(ball2,150)
})
.then(function(){
return promiseAnimate(ball1,150)
})
</script>
</body>
</html>

前面只是初探。
Promise学习什么?

  • ES6的Promise语言标准、Promise/A+ 规范
  • 如何使用
  • 在什么场景使用

Promise是javascript针对异步操作这种场景的解决方案

Javascript异步编程的4种方法

  • 回调函数
  • 事件监听
  • 发布/订阅
  • Promises对象

Promise对象三种状态

  • 未完成(pending)
  • 已完成(fufilled)
  • 失败 (rejected)

参考教程

进击Node.js基础(二)
Javascript异步编程的4种方法
Understanding Async Programming in Node.js

AlexZ33 wechat
扫码订阅公众号
如果文章对您有用请随意打赏,谢谢支持!