回答編集履歴
8
a
test
CHANGED
@@ -126,4 +126,32 @@
|
|
126
126
|
|
127
127
|
})().catch(e => console.error(e));
|
128
128
|
|
129
|
+
|
130
|
+
|
131
|
+
/*
|
132
|
+
|
133
|
+
|
134
|
+
|
135
|
+
coを使うけどよりPromiseやasync/awaitとの親和性を高くして書く場合もこのように
|
136
|
+
|
137
|
+
|
138
|
+
|
139
|
+
co(function* () {
|
140
|
+
|
141
|
+
yield Promise.all(keys.map(value => co(function* () {
|
142
|
+
|
143
|
+
yield sleep(Math.round(Math.random()*10000));
|
144
|
+
|
145
|
+
console.log('done: ' + value);
|
146
|
+
|
147
|
+
})));
|
148
|
+
|
149
|
+
console.log('[all done]');
|
150
|
+
|
151
|
+
}).catch(e => console.error(e));
|
152
|
+
|
153
|
+
|
154
|
+
|
155
|
+
*/
|
156
|
+
|
129
157
|
```
|
7
a
test
CHANGED
@@ -90,4 +90,40 @@
|
|
90
90
|
|
91
91
|
|
92
92
|
|
93
|
+
Babelでコンパイルする前提でasync/awaitを使うなら
|
93
94
|
|
95
|
+
|
96
|
+
|
97
|
+
```JavaScript
|
98
|
+
|
99
|
+
'use strict';
|
100
|
+
|
101
|
+
|
102
|
+
|
103
|
+
function sleep(ms) {
|
104
|
+
|
105
|
+
return new Promise(resolve => setTimeout(resolve, ms));
|
106
|
+
|
107
|
+
}
|
108
|
+
|
109
|
+
|
110
|
+
|
111
|
+
const keys = ['a','b','c','d','e','f','g','h','i','j','k','l'];
|
112
|
+
|
113
|
+
|
114
|
+
|
115
|
+
(async () => {
|
116
|
+
|
117
|
+
await Promise.all(keys.map(async value => {
|
118
|
+
|
119
|
+
await sleep(Math.round(Math.random()*10000));
|
120
|
+
|
121
|
+
console.log('done: ' + value);
|
122
|
+
|
123
|
+
}));
|
124
|
+
|
125
|
+
console.log('[all done]');
|
126
|
+
|
127
|
+
})().catch(e => console.error(e));
|
128
|
+
|
129
|
+
```
|
6
a
test
CHANGED
@@ -74,7 +74,7 @@
|
|
74
74
|
|
75
75
|
co(function* () {
|
76
76
|
|
77
|
-
yield keys.map(
|
77
|
+
yield keys.map(function* (value) {
|
78
78
|
|
79
79
|
yield sleep(Math.round(Math.random()*10000));
|
80
80
|
|
@@ -87,3 +87,7 @@
|
|
87
87
|
}).catch(e => console.error(e));
|
88
88
|
|
89
89
|
```
|
90
|
+
|
91
|
+
|
92
|
+
|
93
|
+
|
5
a
test
CHANGED
@@ -36,7 +36,7 @@
|
|
36
36
|
|
37
37
|
)).then(
|
38
38
|
|
39
|
-
() => console.log('
|
39
|
+
() => console.log('[all done]'), // すべてresolveされた
|
40
40
|
|
41
41
|
e => console.error(e) // ひとつでもrejectされた,または例外がスローされた
|
42
42
|
|
@@ -82,7 +82,7 @@
|
|
82
82
|
|
83
83
|
});
|
84
84
|
|
85
|
-
console.log('
|
85
|
+
console.log('[all done]');
|
86
86
|
|
87
87
|
}).catch(e => console.error(e));
|
88
88
|
|
4
a
test
CHANGED
@@ -7,6 +7,8 @@
|
|
7
7
|
|
8
8
|
|
9
9
|
せっかくなのでES6の機能ふんだんに使って書きます.
|
10
|
+
|
11
|
+
(Node.jsということなので,特にブラウザの互換性のような問題も気にしなくていいと思います)
|
10
12
|
|
11
13
|
|
12
14
|
|
3
a
test
CHANGED
@@ -18,7 +18,7 @@
|
|
18
18
|
|
19
19
|
function sleep(ms) {
|
20
20
|
|
21
|
-
return new Promise(r => setTimeout(r, ms));
|
21
|
+
return new Promise(resolve => setTimeout(resolve, ms));
|
22
22
|
|
23
23
|
}
|
24
24
|
|
@@ -30,9 +30,7 @@
|
|
30
30
|
|
31
31
|
Promise.all(keys.map(
|
32
32
|
|
33
|
-
value => sleep(Math.round(Math.random()*10000))
|
34
|
-
|
35
|
-
|
33
|
+
value => sleep(Math.round(Math.random()*10000)).then(() => console.log('done: ' + value))
|
36
34
|
|
37
35
|
)).then(
|
38
36
|
|
@@ -62,7 +60,7 @@
|
|
62
60
|
|
63
61
|
function sleep(ms) {
|
64
62
|
|
65
|
-
return new Promise(r => setTimeout(r, ms));
|
63
|
+
return new Promise(resolve => setTimeout(resolve, ms));
|
66
64
|
|
67
65
|
}
|
68
66
|
|
2
a
test
CHANGED
@@ -16,30 +16,74 @@
|
|
16
16
|
|
17
17
|
|
18
18
|
|
19
|
+
function sleep(ms) {
|
20
|
+
|
21
|
+
return new Promise(r => setTimeout(r, ms));
|
22
|
+
|
23
|
+
}
|
24
|
+
|
25
|
+
|
26
|
+
|
19
27
|
const keys = ['a','b','c','d','e','f','g','h','i','j','k','l'];
|
20
28
|
|
21
29
|
|
22
30
|
|
23
|
-
Promise.all(
|
31
|
+
Promise.all(keys.map(
|
24
32
|
|
25
|
-
|
33
|
+
value => sleep(Math.round(Math.random()*10000))
|
26
34
|
|
27
|
-
|
35
|
+
.then(() => console.log('done: ' + value))
|
28
36
|
|
29
|
-
console.log('done: ' + value);
|
30
|
-
|
31
|
-
resolve(); // ここではすべてresolveする
|
32
|
-
|
33
|
-
}, Math.round(Math.random()*10000))
|
34
|
-
|
35
|
-
))
|
36
|
-
|
37
|
-
).then(
|
37
|
+
)).then(
|
38
38
|
|
39
39
|
() => console.log('finished'), // すべてresolveされた
|
40
40
|
|
41
|
-
|
41
|
+
e => console.error(e) // ひとつでもrejectされた,または例外がスローされた
|
42
42
|
|
43
43
|
);
|
44
44
|
|
45
45
|
```
|
46
|
+
|
47
|
+
|
48
|
+
|
49
|
+
co+Generatorを使うならもう少し見やすくかけますね.
|
50
|
+
|
51
|
+
|
52
|
+
|
53
|
+
```JavaScript
|
54
|
+
|
55
|
+
'use strict';
|
56
|
+
|
57
|
+
|
58
|
+
|
59
|
+
const co = require('co');
|
60
|
+
|
61
|
+
|
62
|
+
|
63
|
+
function sleep(ms) {
|
64
|
+
|
65
|
+
return new Promise(r => setTimeout(r, ms));
|
66
|
+
|
67
|
+
}
|
68
|
+
|
69
|
+
|
70
|
+
|
71
|
+
const keys = ['a','b','c','d','e','f','g','h','i','j','k','l'];
|
72
|
+
|
73
|
+
|
74
|
+
|
75
|
+
co(function* () {
|
76
|
+
|
77
|
+
yield keys.map(value => function* () {
|
78
|
+
|
79
|
+
yield sleep(Math.round(Math.random()*10000));
|
80
|
+
|
81
|
+
console.log('done: ' + value);
|
82
|
+
|
83
|
+
});
|
84
|
+
|
85
|
+
console.log('finished');
|
86
|
+
|
87
|
+
}).catch(e => console.error(e));
|
88
|
+
|
89
|
+
```
|
1
a
test
CHANGED
@@ -24,16 +24,22 @@
|
|
24
24
|
|
25
25
|
keys.map(value => new Promise(
|
26
26
|
|
27
|
-
resolve => setTimeout(() => {
|
27
|
+
(resolve, reject) => setTimeout(() => {
|
28
28
|
|
29
29
|
console.log('done: ' + value);
|
30
30
|
|
31
|
-
resolve();
|
31
|
+
resolve(); // ここではすべてresolveする
|
32
32
|
|
33
33
|
}, Math.round(Math.random()*10000))
|
34
34
|
|
35
35
|
))
|
36
36
|
|
37
|
+
).then(
|
38
|
+
|
37
|
-
|
39
|
+
() => console.log('finished'), // すべてresolveされた
|
40
|
+
|
41
|
+
() => console.error('failed') // ひとつでもrejectされた,または例外がスローされた
|
42
|
+
|
43
|
+
);
|
38
44
|
|
39
45
|
```
|