Skip to content

Commit fc067b7

Browse files
author
Your Name
committed
flow
1 parent ac82260 commit fc067b7

File tree

3 files changed

+107
-1
lines changed

3 files changed

+107
-1
lines changed

2.promise/generator/3.async.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ let read = promisify(fs.readFile);
99
async function r() {
1010
let b = await Promise.all([read('a.txt', 'utf8'), read('b.txt', 'utf8')]);
1111
return b;
12-
}
12+
};
1313
r().then(data=>{
1414
console.log(data);
1515
});

2.promise/index.html

+36
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
<!DOCTYPE html>
2+
<html lang="en">
3+
<head>
4+
<meta charset="UTF-8">
5+
<meta name="viewport" content="width=device-width, initial-scale=1.0">
6+
<meta http-equiv="X-UA-Compatible" content="ie=edge">
7+
<title>Document</title>
8+
<style>
9+
#box div{
10+
width: 100px;
11+
height: 100px;
12+
background: red;
13+
border-radius: 50%;
14+
position: absolute;
15+
left: 0;
16+
}
17+
#ball1{
18+
top:0
19+
}
20+
#ball2{
21+
top:110px
22+
}
23+
#ball3{
24+
top:220px
25+
}
26+
</style>
27+
</head>
28+
<body>
29+
<div id="box">
30+
<div id="ball1"></div>
31+
<div id="ball2"></div>
32+
<div id="ball3"></div>
33+
</div>
34+
</body>
35+
<script src="index.js"></script>
36+
</html>

2.promise/index.js

+70
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
function move(ele, position) {
2+
return new Promise((resolve, reject) => {
3+
let left = 0;
4+
let timer = setInterval(() => {
5+
left += 5;
6+
if (left >= position) {
7+
clearInterval(timer);
8+
ele.style.transform = `translateX(${position}px)`;
9+
resolve();
10+
} else {
11+
ele.style.transform = `translateX(${left}px)`;
12+
}
13+
}, 15);
14+
})
15+
}
16+
// axios,fetch
17+
// 4) async + await
18+
async function m() {
19+
await move(ball1, 100);
20+
await move(ball2, 100);
21+
await move(ball3, 100);
22+
}
23+
m().then(data=>{
24+
alert('ok');
25+
});
26+
27+
// 3) generator
28+
// function* m() {
29+
// yield move(ball1, 500);
30+
// yield move(ball2, 500);
31+
// yield move(ball3, 500);
32+
// }
33+
// function co(it) {
34+
// return new Promise((resolve, reject) => {
35+
// function next(data) {
36+
// let { done, value } = it.next(data);
37+
// if (done) return resolve(value);
38+
// value.then(data => {
39+
// next(data);
40+
// }, reject);
41+
// }
42+
// next();
43+
// })
44+
// }
45+
46+
47+
// co(m()).then(data => {
48+
// alert('ok');
49+
// })
50+
51+
52+
53+
// 2)promise
54+
// move(ball1,500).then(data=>{
55+
// return move(ball2,500)
56+
// }).then(data=>{
57+
// return move(ball3,200);
58+
// }).then(data=>{
59+
// alert('ok');
60+
// })
61+
62+
63+
// 1)callback
64+
// move(ball1,500,function () {
65+
// move(ball2, 500, function () {
66+
// move(ball3, 500, function () {
67+
// alert('动完了');
68+
// })
69+
// })
70+
// })

0 commit comments

Comments
 (0)