teratail header banner
teratail header banner
質問するログイン新規登録

回答編集履歴

14

a

2018/05/17 10:04

投稿

HayatoKamono
HayatoKamono

スコア2415

answer CHANGED
@@ -43,10 +43,6 @@
43
43
  }
44
44
  }
45
45
 
46
- const tasks = [asyncOrderDrink, asyncPayBill, asyncDrinkCoffee];
47
-
48
- const tasksGenerator = makeLoopGenerator(tasks);
49
-
50
46
  function runLoop(generator) {
51
47
  function loop() {
52
48
  generator.next().value.then(loop);
@@ -54,7 +50,9 @@
54
50
  loop();
55
51
  }
56
52
 
57
- runLoop(tasksGenerator)
53
+ runLoop(
54
+ makeLoopGenerator([asyncOrderDrink, asyncPayBill, asyncDrinkCoffee])
55
+ );
58
56
 
59
57
  ```
60
58
 

13

runLoop(makeLoopGenerator(...funcs))

2018/05/17 10:04

投稿

HayatoKamono
HayatoKamono

スコア2415

answer CHANGED
@@ -189,10 +189,7 @@
189
189
  makeThunk(changeText, 'WORLD', containerB)
190
190
  ];
191
191
 
192
- const generator = makeLoopGenerator(...funcs);
192
+ runLoop(makeLoopGenerator(...funcs));
193
-
194
- runLoop(generator)
195
-
196
193
  }
197
194
 
198
195
  return {

12

setTimeout(runLoop, 1000, generator);

2018/05/17 06:16

投稿

HayatoKamono
HayatoKamono

スコア2415

answer CHANGED
@@ -173,7 +173,7 @@
173
173
 
174
174
  function runLoop(generator) {
175
175
  generator.next();
176
- setTimeout(runLoop.bind(null, generator), 1000)
176
+ setTimeout(runLoop, 1000, generator);
177
177
  }
178
178
 
179
179
  function init() {

11

これで編集完了

2018/05/17 06:12

投稿

HayatoKamono
HayatoKamono

スコア2415

answer CHANGED
@@ -147,59 +147,61 @@
147
147
  ```
148
148
  (function(global, document) {
149
149
 
150
+ function makeThunk(func, ...rest) {
151
+ return function() {
152
+ return func(...rest);
153
+ }
154
+ }
155
+
150
- const app = (function() {
156
+ const app = (function() {
151
157
 
152
- function changeClassName(className, element) {
158
+ function changeClassName(className, element) {
153
- return function() {
154
- element.className = className;
159
+ element.className = className;
155
- }
160
+ }
156
- }
157
161
 
158
- function changeText(value, element) {
162
+ function changeText(value, element) {
159
- return function() {
160
- element.textContent = value;
163
+ element.textContent = value;
161
- }
164
+ }
162
- }
163
165
 
164
- function* makeLoopGenerator(...funcs) {
166
+ function* makeLoopGenerator(...funcs) {
165
- while(true) {
167
+ while(true) {
166
- for (let func of funcs) {
168
+ for (let func of funcs) {
167
- yield func();
169
+ yield func();
168
- }
170
+ }
169
- }
171
+ }
170
- }
172
+ }
171
173
 
172
- function runLoop(generator) {
174
+ function runLoop(generator) {
173
- generator.next();
175
+ generator.next();
174
- setTimeout(runLoop.bind(null, generator), 1000)
176
+ setTimeout(runLoop.bind(null, generator), 1000)
175
- }
177
+ }
176
178
 
177
- function init() {
179
+ function init() {
178
- const containerA = document.getElementById('containerA');
180
+ const containerA = document.getElementById('containerA');
179
- const containerB = document.getElementById('containerB');
181
+ const containerB = document.getElementById('containerB');
180
182
 
181
- const funcs = [
183
+ const funcs = [
182
- changeClassName('spring', containerA),
184
+ makeThunk(changeClassName, 'spring', containerA),
183
- changeClassName('summer', containerA),
185
+ makeThunk(changeClassName, 'summer', containerA),
184
- changeText('HELLO', containerB),
186
+ makeThunk(changeText, 'HELLO', containerB),
185
- changeClassName('autumn', containerA),
187
+ makeThunk(changeClassName, 'autumn', containerA),
186
- changeClassName('winter', containerA),
188
+ makeThunk(changeClassName, 'winter', containerA),
187
- changeText('WORLD', containerB)
189
+ makeThunk(changeText, 'WORLD', containerB)
188
- ];
190
+ ];
189
191
 
190
- const generator = makeLoopGenerator(...funcs);
192
+ const generator = makeLoopGenerator(...funcs);
191
193
 
192
- runLoop(generator)
194
+ runLoop(generator)
193
195
 
194
- }
196
+ }
195
197
 
196
- return {
198
+ return {
197
- init: init
199
+ init: init
198
- }
200
+ }
199
201
 
200
- })();
202
+ })();
201
203
 
202
- document.addEventListener('DOMContentLoaded', app.init);
204
+ document.addEventListener('DOMContentLoaded', app.init);
203
205
 
204
206
 
205
207
  })(window, document);

10

これにて編集終了

2018/05/17 03:30

投稿

HayatoKamono
HayatoKamono

スコア2415

answer CHANGED
@@ -106,7 +106,7 @@
106
106
  ![イメージ説明](8f8da0e073fe063f47696d8c5e07a73b.gif)
107
107
  -> [animationGifで再生](https://teratail.storage.googleapis.com/uploads/contributed_images/8f8da0e073fe063f47696d8c5e07a73b.gif)
108
108
 
109
- 質問者さんの[前回の質問](https://teratail.com/questions/126488)の場合は、一定間隔で複数の(異なる)処理を実行させたいというものだったので、`Promise`を使わずに単に、`setTimeout()`関数で定期実行させてあげるだけで良いかと思います。
109
+ 質問者さんの[前回の質問](https://teratail.com/questions/126488)の場合は、一定間隔で複数の(異なる)処理を実行させたいというものだったので、`Promise`を使わずに単に、`setTimeout()`関数で定期実行させてあげるだけで良いかと思います。
110
110
 
111
111
  以下、[前回の質問](https://teratail.com/questions/126488)の「異なる複数の処理」に対応したコードです。
112
112
 
@@ -146,66 +146,62 @@
146
146
  ## JavaScript
147
147
  ```
148
148
  (function(global, document) {
149
-
150
- const app = (function() {
151
-
152
- function changeClassName(value) {
153
- return function(element) {
154
- return function() {
155
- element.className = value;
156
- }
157
- }
158
- }
159
149
 
160
- function changeText(value) {
161
- return function(element) {
162
- return function() {
150
+ const app = (function() {
163
- element.textContent = value;
164
- }
165
- }
166
- }
167
151
 
168
- function* makeLoopGenerator(...funcs) {
152
+ function changeClassName(className, element) {
169
- while(true) {
170
- for (let func of funcs) {
153
+ return function() {
171
- yield func();
154
+ element.className = className;
172
- }
155
+ }
173
- }
156
+ }
174
- }
175
-
176
- function loop(generator) {
177
- generator.next();
178
- setTimeout(loop.bind(null, generator), 1000)
179
- }
180
-
181
- function init() {
182
- const containerA = document.getElementById('containerA');
183
- const containerB = document.getElementById('containerB');
184
-
185
- const funcs = [
186
- changeClassName('spring')(containerA),
187
- changeClassName('summer')(containerA),
188
- changeText('HELLO')(containerB),
189
- changeClassName('autumn')(containerA),
190
- changeClassName('winter')(containerA),
191
- changeText('WORLD')(containerB),
192
- ];
193
-
194
- const generator = makeLoopGenerator(...funcs);
195
157
 
158
+ function changeText(value, element) {
196
- loop(generator)
159
+ return function() {
197
-
160
+ element.textContent = value;
198
- }
161
+ }
199
-
200
- return {
201
- init: init
202
- }
162
+ }
203
-
204
- })();
205
-
206
- document.addEventListener('DOMContentLoaded', app.init);
207
163
 
208
-
164
+ function* makeLoopGenerator(...funcs) {
165
+ while(true) {
166
+ for (let func of funcs) {
167
+ yield func();
168
+ }
169
+ }
170
+ }
171
+
172
+ function runLoop(generator) {
173
+ generator.next();
174
+ setTimeout(runLoop.bind(null, generator), 1000)
175
+ }
176
+
177
+ function init() {
178
+ const containerA = document.getElementById('containerA');
179
+ const containerB = document.getElementById('containerB');
180
+
181
+ const funcs = [
182
+ changeClassName('spring', containerA),
183
+ changeClassName('summer', containerA),
184
+ changeText('HELLO', containerB),
185
+ changeClassName('autumn', containerA),
186
+ changeClassName('winter', containerA),
187
+ changeText('WORLD', containerB)
188
+ ];
189
+
190
+ const generator = makeLoopGenerator(...funcs);
191
+
192
+ runLoop(generator)
193
+
194
+ }
195
+
196
+ return {
197
+ init: init
198
+ }
199
+
200
+ })();
201
+
202
+ document.addEventListener('DOMContentLoaded', app.init);
203
+
204
+
209
205
  })(window, document);
210
206
  ```
211
207
 
@@ -213,10 +209,7 @@
213
209
 
214
210
  回答に載せたコードでもエラー対応をするためのコードは省略していますが、エラーが発生する可能性があるのであれば、`new Promise()`に渡すコールバック関数内で、非同期処理が失敗した時に`reject()`が実行されるようにコードを書いてあげる必要があります。
215
211
 
216
- # 補足
217
212
 
218
- ※ 一応動作確認をしてからコードを載せていますが、荒く書いているのでおかしいところがあるかもしれません。ご了承ください。。。(あとで修正・補足などするかもしれません)
219
-
220
213
  # 参考
221
214
  掲載コードの中で`generator`、`spread syntax`、`rest parameters`を使っていますが、詳細やブラウザ対応状況は以下でご確認ください。(英語記事へのリンクになっていますが、リンク先ページで言語切り替えが可能です。)
222
215
 

9

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Spread_syntax

2018/05/17 03:09

投稿

HayatoKamono
HayatoKamono

スコア2415

answer CHANGED
@@ -218,7 +218,8 @@
218
218
  ※ 一応動作確認をしてからコードを載せていますが、荒く書いているのでおかしいところがあるかもしれません。ご了承ください。。。(あとで修正・補足などするかもしれません)
219
219
 
220
220
  # 参考
221
- 掲載コードの中で`generator``rest parameters`を使っていますが、ブラウザ対応状況は以下でご確認ください。(英語記事へのリンクになっていますが、時間ある時に日本語ページへリンクを変ると思います。)
221
+ 掲載コードの中で`generator``spread syntax`、`rest parameters`を使っていますが、詳細やブラウザ対応状況は以下でご確認ください。(英語記事へのリンクになっていますが、リンク先ページで言語切り替が可能です。)
222
222
 
223
223
  - [Generator](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Generator)
224
+ - [Spread Syntax](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Spread_syntax)
224
225
  - [Rest Parameters](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Functions/rest_parameters)

8

質問への個別回答を追記

2018/05/17 02:33

投稿

HayatoKamono
HayatoKamono

スコア2415

answer CHANGED
@@ -1,3 +1,9 @@
1
+ # Q1. 「時間差で実行、さらに繰り返し」処理を行うには?
2
+
3
+ とりあえず、`Promise`を使う前提で書いて見たコードが以下となります。
4
+
5
+ ## Pattern A
6
+
1
7
  ![イメージ説明](33a9321d33c19a67225b30098105d4b9.gif)
2
8
  → [animationGifで再生 ](https://teratail.storage.googleapis.com/uploads/contributed_images/33a9321d33c19a67225b30098105d4b9.gif)
3
9
 
@@ -53,7 +59,7 @@
53
59
  ```
54
60
 
55
61
 
56
- # 追記
62
+ # Pattern B(追記)
57
63
 
58
64
  質問者さんの前回の質問を見る限りでは、単に時間差で複数の同期処理を実行させたいように見受けられるので、`maisumakun`さんの回答にあるように`sleep`関数のようなものを作って、各同期処理の前で実行してあげるのが良さそうです。
59
65
 
@@ -92,4 +98,127 @@
92
98
  runLoop(makeLoopGenerator())
93
99
  ```
94
100
 
95
- ※ 追記のコードも最初のコードのように、ループ内で実行させたい関数が可変の場合でも対応できるようにしたかったですが、時間切れの為、非対応としました。
101
+ ※ 追記のコードも最初のコードのように、ループ内で実行させたい関数が可変の場合でも対応できるようにしたかったですが、時間切れの為、非対応としました。
102
+
103
+
104
+ # Q2. Promiseで処理をする選択は良いのか?
105
+
106
+ ![イメージ説明](8f8da0e073fe063f47696d8c5e07a73b.gif)
107
+ -> [animationGifで再生](https://teratail.storage.googleapis.com/uploads/contributed_images/8f8da0e073fe063f47696d8c5e07a73b.gif)
108
+
109
+ 質問者さんの[前回の質問](https://teratail.com/questions/126488)の場合は、一定間隔で複数の(異なる)処理を実行させたいというものだったので、`Promise`を使わずに単に、`setTimeout()`関数で定期実行させてあげるだけで良いかと思います。
110
+
111
+ 以下、[前回の質問](https://teratail.com/questions/126488)の「異なる複数の処理」に対応したコードです。
112
+
113
+ 以下を1000秒毎に実行
114
+
115
+ - 背景色を変える関数を実行
116
+ - 背景色を変える関数を実行
117
+ - テキストを変える関数を実行
118
+ - 背景色を変える関数を実行
119
+ - 背景色を変える関数を実行
120
+ - テキストを変える関数を実行
121
+ - 上記の繰り返し
122
+
123
+ ## html
124
+ ```
125
+ <div id="containerA" class="spring"></div>
126
+ <div id="containerB">-----</div>
127
+ ```
128
+
129
+ ## CSS
130
+ ```
131
+ #containerA { width: 100px; height: 100px; }
132
+
133
+ #containerB {
134
+ width: 100px;
135
+ background: gray;
136
+ font-size: 20px;
137
+ text-align: center;
138
+ }
139
+
140
+ .spring { background-color: pink; }
141
+ .summer { background-color: red; }
142
+ .autumn { background-color: brown; }
143
+ .winter { background-color: blue; }
144
+ ```
145
+
146
+ ## JavaScript
147
+ ```
148
+ (function(global, document) {
149
+
150
+ const app = (function() {
151
+
152
+ function changeClassName(value) {
153
+ return function(element) {
154
+ return function() {
155
+ element.className = value;
156
+ }
157
+ }
158
+ }
159
+
160
+ function changeText(value) {
161
+ return function(element) {
162
+ return function() {
163
+ element.textContent = value;
164
+ }
165
+ }
166
+ }
167
+
168
+ function* makeLoopGenerator(...funcs) {
169
+ while(true) {
170
+ for (let func of funcs) {
171
+ yield func();
172
+ }
173
+ }
174
+ }
175
+
176
+ function loop(generator) {
177
+ generator.next();
178
+ setTimeout(loop.bind(null, generator), 1000)
179
+ }
180
+
181
+ function init() {
182
+ const containerA = document.getElementById('containerA');
183
+ const containerB = document.getElementById('containerB');
184
+
185
+ const funcs = [
186
+ changeClassName('spring')(containerA),
187
+ changeClassName('summer')(containerA),
188
+ changeText('HELLO')(containerB),
189
+ changeClassName('autumn')(containerA),
190
+ changeClassName('winter')(containerA),
191
+ changeText('WORLD')(containerB),
192
+ ];
193
+
194
+ const generator = makeLoopGenerator(...funcs);
195
+
196
+ loop(generator)
197
+
198
+ }
199
+
200
+ return {
201
+ init: init
202
+ }
203
+
204
+ })();
205
+
206
+ document.addEventListener('DOMContentLoaded', app.init);
207
+
208
+
209
+ })(window, document);
210
+ ```
211
+
212
+ # Q3. そもそも私のPromiseの記述に誤りが無いか、もしくはもっと良い記述があればご指摘いただけると助かります
213
+
214
+ 回答に載せたコードでもエラー対応をするためのコードは省略していますが、エラーが発生する可能性があるのであれば、`new Promise()`に渡すコールバック関数内で、非同期処理が失敗した時に`reject()`が実行されるようにコードを書いてあげる必要があります。
215
+
216
+ # 補足
217
+
218
+ ※ 一応動作確認をしてからコードを載せていますが、荒く書いているのでおかしいところがあるかもしれません。ご了承ください。。。(あとで修正・補足などするかもしれません)
219
+
220
+ # 参考
221
+ 掲載コードの中で`generator`と`rest parameters`を使っていますが、ブラウザ対応状況は以下でご確認ください。(英語記事へのリンクになっていますが、時間ある時に日本語ページへリンクを変えると思います。)
222
+
223
+ - [Generator](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Generator)
224
+ - [Rest Parameters](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Functions/rest_parameters)

7

makeLoopGenerator

2018/05/17 02:30

投稿

HayatoKamono
HayatoKamono

スコア2415

answer CHANGED
@@ -78,7 +78,7 @@
78
78
  loop();
79
79
  }
80
80
 
81
- function* generatorFn() {
81
+ function* makeLoopGenerator() {
82
82
  while(true) {
83
83
  yield sleep(1000);
84
84
  orderCoffee('コーヒーを注文する');
@@ -89,7 +89,7 @@
89
89
  }
90
90
  }
91
91
 
92
- runLoop(generatorFn())
92
+ runLoop(makeLoopGenerator())
93
93
  ```
94
94
 
95
95
  ※ 追記のコードも最初のコードのように、ループ内で実行させたい関数が可変の場合でも対応できるようにしたかったですが、時間切れの為、非対応としました。

6

https://teratail.storage.googleapis.com/uploads/contributed_images/33a9321d33c19a67225b30098105d4b9.

2018/05/17 01:51

投稿

HayatoKamono
HayatoKamono

スコア2415

answer CHANGED
@@ -1,64 +1,58 @@
1
- ![イメージ説明](8d64f96d231388f602f7a4ab286fc3ad.gif)
1
+ ![イメージ説明](33a9321d33c19a67225b30098105d4b9.gif)
2
- [アニメーションGifで再生](https://teratail.storage.googleapis.com/uploads/contributed_images/8d64f96d231388f602f7a4ab286fc3ad.gif)
2
+ [animationGifで再生 ](https://teratail.storage.googleapis.com/uploads/contributed_images/33a9321d33c19a67225b30098105d4b9.gif)
3
3
 
4
4
  ```
5
- const asyncDrink = () => {
5
+ const asyncOrderDrink = () => {
6
- return new Promise((resolve, reject) => {
6
+ return new Promise(resolve => {
7
7
  setTimeout(() => {
8
- console.log('drink')
8
+ console.log('コーヒーを注文する')
9
- resolve('drink');
9
+ resolve();
10
10
  }, 1000)
11
11
  });
12
12
  }
13
13
 
14
- const asyncEat = () => {
14
+ const asyncPayBill = () => {
15
- return new Promise((resolve, reject) => {
15
+ return new Promise(resolve => {
16
16
  setTimeout(() => {
17
- console.log('eat')
17
+ console.log('料金を支払う')
18
- resolve('eat');
18
+ resolve();
19
19
  }, 2000)
20
20
  });
21
21
  }
22
22
 
23
- const asyncSleep = () => {
23
+ const asyncDrinkCoffee = () => {
24
- return new Promise((resolve, reject) => {
24
+ return new Promise(resolve => {
25
25
  setTimeout(() => {
26
- console.log('sleep')
26
+ console.log('コーヒーを飲む')
27
- resolve('sleep');
27
+ resolve();
28
28
  }, 3000)
29
29
  });
30
30
  }
31
31
 
32
- const funcs = [asyncDrink, asyncEat, asyncSleep];
33
-
34
- function generatorFn(fns) {
32
+ function* makeLoopGenerator (fns) {
35
- return function* () {
36
- while(true) {
33
+ while(true) {
37
- for (var fn of fns) {
34
+ for (var fn of fns) {
38
- yield fn();
35
+ yield fn();
39
- }
40
36
  }
41
37
  }
42
38
  }
43
39
 
44
- const g = generatorFn(funcs)();
40
+ const tasks = [asyncOrderDrink, asyncPayBill, asyncDrinkCoffee];
45
41
 
42
+ const tasksGenerator = makeLoopGenerator(tasks);
43
+
46
- function loop(gen) {
44
+ function runLoop(generator) {
47
- function _loop() {
45
+ function loop() {
48
- gen.next().value.then(v => _loop());
46
+ generator.next().value.then(loop);
49
47
  }
50
-
51
- _loop();
48
+ loop();
52
49
  }
53
50
 
54
- loop(g)
51
+ runLoop(tasksGenerator)
55
52
 
56
53
  ```
57
54
 
58
- とりあえず一旦、雑なコードですが置いておきます。
59
- あとで時間のある時に修正・補足等加えると思います。
60
55
 
61
-
62
56
  # 追記
63
57
 
64
58
  質問者さんの前回の質問を見る限りでは、単に時間差で複数の同期処理を実行させたいように見受けられるので、`maisumakun`さんの回答にあるように`sleep`関数のようなものを作って、各同期処理の前で実行してあげるのが良さそうです。
@@ -68,10 +62,8 @@
68
62
 
69
63
  ```
70
64
  const sleep = (delay) => {
71
- return new Promise((resolve, reject) => {
65
+ return new Promise((resolve) => {
72
- setTimeout(() => {
66
+ setTimeout(resolve, delay)
73
- resolve();
74
- }, delay)
75
67
  });
76
68
  }
77
69
 
@@ -81,7 +73,7 @@
81
73
 
82
74
  function runLoop(gen) {
83
75
  function loop() {
84
- gen.next().value.then(v => loop());
76
+ gen.next().value.then(loop);
85
77
  }
86
78
  loop();
87
79
  }
@@ -98,4 +90,6 @@
98
90
  }
99
91
 
100
92
  runLoop(generatorFn())
101
- ```
93
+ ```
94
+
95
+ ※ 追記のコードも最初のコードのように、ループ内で実行させたい関数が可変の場合でも対応できるようにしたかったですが、時間切れの為、非対応としました。

5

a

2018/05/17 01:49

投稿

HayatoKamono
HayatoKamono

スコア2415

answer CHANGED
@@ -86,7 +86,7 @@
86
86
  loop();
87
87
  }
88
88
 
89
- function* generatorFn(list) {
89
+ function* generatorFn() {
90
90
  while(true) {
91
91
  yield sleep(1000);
92
92
  orderCoffee('コーヒーを注文する');

4

a

2018/05/17 01:20

投稿

HayatoKamono
HayatoKamono

スコア2415

answer CHANGED
@@ -89,11 +89,11 @@
89
89
  function* generatorFn(list) {
90
90
  while(true) {
91
91
  yield sleep(1000);
92
- orderCoffee('チャイティラテ');
92
+ orderCoffee('ヒーを注文する');
93
93
  yield sleep(2000);
94
- payBill('480円');
94
+ payBill('料金を支払う');
95
95
  yield sleep(3000);
96
- drinkCoffee('50ml');
96
+ drinkCoffee('コーヒーを飲む');
97
97
  }
98
98
  }
99
99
 

3

a

2018/05/17 01:16

投稿

HayatoKamono
HayatoKamono

スコア2415

answer CHANGED
@@ -61,10 +61,11 @@
61
61
 
62
62
  # 追記
63
63
 
64
- 質問者さんの前回の質問を見る限りでは、単に時間差で複数の同期処理を実行させたいように見受けられるので、`maisuma-kun`さんの回答にあるように`sleep`関数のようなものを作って、各同期処理の前で実行してあげるのが良さそうです。
64
+ 質問者さんの前回の質問を見る限りでは、単に時間差で複数の同期処理を実行させたいように見受けられるので、`maisumakun`さんの回答にあるように`sleep`関数のようなものを作って、各同期処理の前で実行してあげるのが良さそうです。
65
65
 
66
- ということで、`maisuma-kun`さんの回答を参考に以下、`generator`版のコードを追加。
66
+ ということで、`maisumakun`さんの回答を参考に以下、`generator`版のコードを追加。
67
67
 
68
+
68
69
  ```
69
70
  const sleep = (delay) => {
70
71
  return new Promise((resolve, reject) => {

2

a

2018/05/17 01:11

投稿

HayatoKamono
HayatoKamono

スコア2415

answer CHANGED
@@ -56,4 +56,45 @@
56
56
  ```
57
57
 
58
58
  とりあえず一旦、雑なコードですが置いておきます。
59
- あとで時間のある時に修正・補足等加えると思います。
59
+ あとで時間のある時に修正・補足等加えると思います。
60
+
61
+
62
+ # 追記
63
+
64
+ 質問者さんの前回の質問を見る限りでは、単に時間差で複数の同期処理を実行させたいように見受けられるので、`maisuma-kun`さんの回答にあるように`sleep`関数のようなものを作って、各同期処理の前で実行してあげるのが良さそうです。
65
+
66
+ ということで、`maisuma-kun`さんの回答を参考に以下、`generator`版のコードを追加。
67
+
68
+ ```
69
+ const sleep = (delay) => {
70
+ return new Promise((resolve, reject) => {
71
+ setTimeout(() => {
72
+ resolve();
73
+ }, delay)
74
+ });
75
+ }
76
+
77
+ const orderCoffee = (v) => console.log(v);
78
+ const payBill = (v) => console.log(v);
79
+ const drinkCoffee = (v) => console.log(v);
80
+
81
+ function runLoop(gen) {
82
+ function loop() {
83
+ gen.next().value.then(v => loop());
84
+ }
85
+ loop();
86
+ }
87
+
88
+ function* generatorFn(list) {
89
+ while(true) {
90
+ yield sleep(1000);
91
+ orderCoffee('チャイティーラテ');
92
+ yield sleep(2000);
93
+ payBill('480円');
94
+ yield sleep(3000);
95
+ drinkCoffee('50ml');
96
+ }
97
+ }
98
+
99
+ runLoop(generatorFn())
100
+ ```

1

a

2018/05/17 01:09

投稿

HayatoKamono
HayatoKamono

スコア2415

answer CHANGED
@@ -1,5 +1,5 @@
1
1
  ![イメージ説明](8d64f96d231388f602f7a4ab286fc3ad.gif)
2
- アニメーションGif
2
+ [→ アニメーションGifで再生](https://teratail.storage.googleapis.com/uploads/contributed_images/8d64f96d231388f602f7a4ab286fc3ad.gif)
3
3
 
4
4
  ```
5
5
  const asyncDrink = () => {
@@ -56,4 +56,4 @@
56
56
  ```
57
57
 
58
58
  とりあえず一旦、雑なコードですが置いておきます。
59
- あとで修正・補足等加えると思います。
59
+ あとで時間のある時に修正・補足等加えると思います。