回答編集履歴

14

a

2018/05/17 10:04

投稿

HayatoKamono
HayatoKamono

スコア2415

test CHANGED
@@ -88,14 +88,6 @@
88
88
 
89
89
 
90
90
 
91
- const tasks = [asyncOrderDrink, asyncPayBill, asyncDrinkCoffee];
92
-
93
-
94
-
95
- const tasksGenerator = makeLoopGenerator(tasks);
96
-
97
-
98
-
99
91
  function runLoop(generator) {
100
92
 
101
93
  function loop() {
@@ -110,7 +102,11 @@
110
102
 
111
103
 
112
104
 
113
- runLoop(tasksGenerator)
105
+ runLoop(
106
+
107
+ makeLoopGenerator([asyncOrderDrink, asyncPayBill, asyncDrinkCoffee])
108
+
109
+ );
114
110
 
115
111
 
116
112
 

13

runLoop(makeLoopGenerator(...funcs))

2018/05/17 10:04

投稿

HayatoKamono
HayatoKamono

スコア2415

test CHANGED
@@ -380,13 +380,7 @@
380
380
 
381
381
 
382
382
 
383
- const generator = makeLoopGenerator(...funcs);
383
+ runLoop(makeLoopGenerator(...funcs));
384
-
385
-
386
-
387
- runLoop(generator)
388
-
389
-
390
384
 
391
385
  }
392
386
 

12

setTimeout(runLoop, 1000, generator);

2018/05/17 06:16

投稿

HayatoKamono
HayatoKamono

スコア2415

test CHANGED
@@ -348,7 +348,7 @@
348
348
 
349
349
  generator.next();
350
350
 
351
- setTimeout(runLoop.bind(null, generator), 1000)
351
+ setTimeout(runLoop, 1000, generator);
352
352
 
353
353
  }
354
354
 

11

これで編集完了

2018/05/17 06:12

投稿

HayatoKamono
HayatoKamono

スコア2415

test CHANGED
@@ -296,111 +296,115 @@
296
296
 
297
297
 
298
298
 
299
- const app = (function() {
300
-
301
-
302
-
303
- function changeClassName(className, element) {
304
-
305
- return function() {
306
-
307
- element.className = className;
308
-
309
- }
310
-
311
- }
312
-
313
-
314
-
315
- function changeText(value, element) {
316
-
317
- return function() {
318
-
319
- element.textContent = value;
320
-
321
- }
322
-
323
- }
324
-
325
-
326
-
327
- function* makeLoopGenerator(...funcs) {
328
-
329
- while(true) {
330
-
331
- for (let func of funcs) {
332
-
333
- yield func();
334
-
335
- }
336
-
337
- }
338
-
339
- }
340
-
341
-
342
-
343
- function runLoop(generator) {
344
-
345
- generator.next();
346
-
347
- setTimeout(runLoop.bind(null, generator), 1000)
348
-
349
- }
350
-
351
-
352
-
353
- function init() {
354
-
355
- const containerA = document.getElementById('containerA');
356
-
357
- const containerB = document.getElementById('containerB');
358
-
359
-
360
-
361
- const funcs = [
362
-
363
- changeClassName('spring', containerA),
364
-
365
- changeClassName('summer', containerA),
366
-
367
- changeText('HELLO', containerB),
368
-
369
- changeClassName('autumn', containerA),
370
-
371
- changeClassName('winter', containerA),
372
-
373
- changeText('WORLD', containerB)
374
-
375
- ];
376
-
377
-
378
-
379
- const generator = makeLoopGenerator(...funcs);
380
-
381
-
382
-
383
- runLoop(generator)
384
-
385
-
386
-
387
- }
388
-
389
-
390
-
391
- return {
392
-
393
- init: init
394
-
395
- }
396
-
397
-
398
-
399
- })();
400
-
401
-
402
-
403
- document.addEventListener('DOMContentLoaded', app.init);
299
+ function makeThunk(func, ...rest) {
300
+
301
+ return function() {
302
+
303
+ return func(...rest);
304
+
305
+ }
306
+
307
+ }
308
+
309
+
310
+
311
+ const app = (function() {
312
+
313
+
314
+
315
+ function changeClassName(className, element) {
316
+
317
+ element.className = className;
318
+
319
+ }
320
+
321
+
322
+
323
+ function changeText(value, element) {
324
+
325
+ element.textContent = value;
326
+
327
+ }
328
+
329
+
330
+
331
+ function* makeLoopGenerator(...funcs) {
332
+
333
+ while(true) {
334
+
335
+ for (let func of funcs) {
336
+
337
+ yield func();
338
+
339
+ }
340
+
341
+ }
342
+
343
+ }
344
+
345
+
346
+
347
+ function runLoop(generator) {
348
+
349
+ generator.next();
350
+
351
+ setTimeout(runLoop.bind(null, generator), 1000)
352
+
353
+ }
354
+
355
+
356
+
357
+ function init() {
358
+
359
+ const containerA = document.getElementById('containerA');
360
+
361
+ const containerB = document.getElementById('containerB');
362
+
363
+
364
+
365
+ const funcs = [
366
+
367
+ makeThunk(changeClassName, 'spring', containerA),
368
+
369
+ makeThunk(changeClassName, 'summer', containerA),
370
+
371
+ makeThunk(changeText, 'HELLO', containerB),
372
+
373
+ makeThunk(changeClassName, 'autumn', containerA),
374
+
375
+ makeThunk(changeClassName, 'winter', containerA),
376
+
377
+ makeThunk(changeText, 'WORLD', containerB)
378
+
379
+ ];
380
+
381
+
382
+
383
+ const generator = makeLoopGenerator(...funcs);
384
+
385
+
386
+
387
+ runLoop(generator)
388
+
389
+
390
+
391
+ }
392
+
393
+
394
+
395
+ return {
396
+
397
+ init: init
398
+
399
+ }
400
+
401
+
402
+
403
+ })();
404
+
405
+
406
+
407
+ document.addEventListener('DOMContentLoaded', app.init);
404
408
 
405
409
 
406
410
 

10

これにて編集終了

2018/05/17 03:30

投稿

HayatoKamono
HayatoKamono

スコア2415

test CHANGED
@@ -214,7 +214,7 @@
214
214
 
215
215
 
216
216
 
217
- 質問者さんの[前回の質問](https://teratail.com/questions/126488)の場合は、一定間隔で複数の(異なる)処理を実行させたいというものだったので、`Promise`を使わずに単に、`setTimeout()`関数で定期実行させてあげるだけで良いかと思います。
217
+ 質問者さんの[前回の質問](https://teratail.com/questions/126488)の場合は、一定間隔で複数の(異なる)処理を実行させたいというものだったので、`Promise`を使わずに単に、`setTimeout()`関数で定期実行させてあげるだけで良いかと思います。
218
218
 
219
219
 
220
220
 
@@ -294,125 +294,117 @@
294
294
 
295
295
  (function(global, document) {
296
296
 
297
-
298
-
299
- const app = (function() {
300
-
301
-
302
-
303
- function changeClassName(value) {
304
-
305
- return function(element) {
306
-
307
- return function() {
308
-
309
- element.className = value;
310
-
311
- }
312
-
313
- }
314
-
315
- }
316
-
317
-
318
-
319
- function changeText(value) {
320
-
321
- return function(element) {
322
-
323
- return function() {
324
-
325
- element.textContent = value;
326
-
327
- }
328
-
329
- }
330
-
331
- }
332
-
333
-
334
-
335
- function* makeLoopGenerator(...funcs) {
336
-
337
- while(true) {
338
-
339
- for (let func of funcs) {
340
-
341
- yield func();
342
-
343
- }
344
-
345
- }
346
-
347
- }
348
-
349
-
350
-
351
- function loop(generator) {
352
-
353
- generator.next();
354
-
355
- setTimeout(loop.bind(null, generator), 1000)
356
-
357
- }
358
-
359
-
360
-
361
- function init() {
362
-
363
- const containerA = document.getElementById('containerA');
364
-
365
- const containerB = document.getElementById('containerB');
366
-
367
-
368
-
369
- const funcs = [
370
-
371
- changeClassName('spring')(containerA),
372
-
373
- changeClassName('summer')(containerA),
374
-
375
- changeText('HELLO')(containerB),
376
-
377
- changeClassName('autumn')(containerA),
378
-
379
- changeClassName('winter')(containerA),
380
-
381
- changeText('WORLD')(containerB),
382
-
383
- ];
384
-
385
-
386
-
387
- const generator = makeLoopGenerator(...funcs);
388
-
389
-
390
-
391
- loop(generator)
392
-
393
-
394
-
395
- }
396
-
397
-
398
-
399
- return {
400
-
401
- init: init
402
-
403
- }
404
-
405
-
406
-
407
- })();
408
-
409
-
410
-
411
- document.addEventListener('DOMContentLoaded', app.init);
412
-
413
-
414
-
415
-
297
+
298
+
299
+ const app = (function() {
300
+
301
+
302
+
303
+ function changeClassName(className, element) {
304
+
305
+ return function() {
306
+
307
+ element.className = className;
308
+
309
+ }
310
+
311
+ }
312
+
313
+
314
+
315
+ function changeText(value, element) {
316
+
317
+ return function() {
318
+
319
+ element.textContent = value;
320
+
321
+ }
322
+
323
+ }
324
+
325
+
326
+
327
+ function* makeLoopGenerator(...funcs) {
328
+
329
+ while(true) {
330
+
331
+ for (let func of funcs) {
332
+
333
+ yield func();
334
+
335
+ }
336
+
337
+ }
338
+
339
+ }
340
+
341
+
342
+
343
+ function runLoop(generator) {
344
+
345
+ generator.next();
346
+
347
+ setTimeout(runLoop.bind(null, generator), 1000)
348
+
349
+ }
350
+
351
+
352
+
353
+ function init() {
354
+
355
+ const containerA = document.getElementById('containerA');
356
+
357
+ const containerB = document.getElementById('containerB');
358
+
359
+
360
+
361
+ const funcs = [
362
+
363
+ changeClassName('spring', containerA),
364
+
365
+ changeClassName('summer', containerA),
366
+
367
+ changeText('HELLO', containerB),
368
+
369
+ changeClassName('autumn', containerA),
370
+
371
+ changeClassName('winter', containerA),
372
+
373
+ changeText('WORLD', containerB)
374
+
375
+ ];
376
+
377
+
378
+
379
+ const generator = makeLoopGenerator(...funcs);
380
+
381
+
382
+
383
+ runLoop(generator)
384
+
385
+
386
+
387
+ }
388
+
389
+
390
+
391
+ return {
392
+
393
+ init: init
394
+
395
+ }
396
+
397
+
398
+
399
+ })();
400
+
401
+
402
+
403
+ document.addEventListener('DOMContentLoaded', app.init);
404
+
405
+
406
+
407
+
416
408
 
417
409
  })(window, document);
418
410
 
@@ -428,12 +420,6 @@
428
420
 
429
421
 
430
422
 
431
- # 補足
432
-
433
-
434
-
435
- ※ 一応動作確認をしてからコードを載せていますが、荒く書いているのでおかしいところがあるかもしれません。ご了承ください。。。(あとで修正・補足などするかもしれません)
436
-
437
423
 
438
424
 
439
425
  # 参考

9

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

2018/05/17 03:09

投稿

HayatoKamono
HayatoKamono

スコア2415

test CHANGED
@@ -438,10 +438,12 @@
438
438
 
439
439
  # 参考
440
440
 
441
- 掲載コードの中で`generator``rest parameters`を使っていますが、ブラウザ対応状況は以下でご確認ください。(英語記事へのリンクになっていますが、時間ある時に日本語ページへリンクを変ると思います。)
441
+ 掲載コードの中で`generator``spread syntax`、`rest parameters`を使っていますが、詳細やブラウザ対応状況は以下でご確認ください。(英語記事へのリンクになっていますが、リンク先ページで言語切り替が可能です。)
442
442
 
443
443
 
444
444
 
445
445
  - [Generator](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Generator)
446
446
 
447
+ - [Spread Syntax](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Spread_syntax)
448
+
447
449
  - [Rest Parameters](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Functions/rest_parameters)

8

質問への個別回答を追記

2018/05/17 02:33

投稿

HayatoKamono
HayatoKamono

スコア2415

test CHANGED
@@ -1,3 +1,15 @@
1
+ # Q1. 「時間差で実行、さらに繰り返し」処理を行うには?
2
+
3
+
4
+
5
+ とりあえず、`Promise`を使う前提で書いて見たコードが以下となります。
6
+
7
+
8
+
9
+ ## Pattern A
10
+
11
+
12
+
1
13
  ![イメージ説明](33a9321d33c19a67225b30098105d4b9.gif)
2
14
 
3
15
  → [animationGifで再生 ](https://teratail.storage.googleapis.com/uploads/contributed_images/33a9321d33c19a67225b30098105d4b9.gif)
@@ -108,7 +120,7 @@
108
120
 
109
121
 
110
122
 
111
- # 追記
123
+ # Pattern B(追記)
112
124
 
113
125
 
114
126
 
@@ -187,3 +199,249 @@
187
199
 
188
200
 
189
201
  ※ 追記のコードも最初のコードのように、ループ内で実行させたい関数が可変の場合でも対応できるようにしたかったですが、時間切れの為、非対応としました。
202
+
203
+
204
+
205
+
206
+
207
+ # Q2. Promiseで処理をする選択は良いのか?
208
+
209
+
210
+
211
+ ![イメージ説明](8f8da0e073fe063f47696d8c5e07a73b.gif)
212
+
213
+ -> [animationGifで再生](https://teratail.storage.googleapis.com/uploads/contributed_images/8f8da0e073fe063f47696d8c5e07a73b.gif)
214
+
215
+
216
+
217
+ 質問者さんの[前回の質問](https://teratail.com/questions/126488)の場合は、一定間隔で複数の(異なる)処理を実行させたいというものだったので、`Promise`を使わずに単に、`setTimeout()`関数で定期実行させてあげるだけで良いかと思います。
218
+
219
+
220
+
221
+ 以下、[前回の質問](https://teratail.com/questions/126488)の「異なる複数の処理」に対応したコードです。
222
+
223
+
224
+
225
+ 以下を1000秒毎に実行
226
+
227
+
228
+
229
+ - 背景色を変える関数を実行
230
+
231
+ - 背景色を変える関数を実行
232
+
233
+ - テキストを変える関数を実行
234
+
235
+ - 背景色を変える関数を実行
236
+
237
+ - 背景色を変える関数を実行
238
+
239
+ - テキストを変える関数を実行
240
+
241
+ - 上記の繰り返し
242
+
243
+
244
+
245
+ ## html
246
+
247
+ ```
248
+
249
+ <div id="containerA" class="spring"></div>
250
+
251
+ <div id="containerB">-----</div>
252
+
253
+ ```
254
+
255
+
256
+
257
+ ## CSS
258
+
259
+ ```
260
+
261
+ #containerA { width: 100px; height: 100px; }
262
+
263
+
264
+
265
+ #containerB {
266
+
267
+ width: 100px;
268
+
269
+ background: gray;
270
+
271
+ font-size: 20px;
272
+
273
+ text-align: center;
274
+
275
+ }
276
+
277
+
278
+
279
+ .spring { background-color: pink; }
280
+
281
+ .summer { background-color: red; }
282
+
283
+ .autumn { background-color: brown; }
284
+
285
+ .winter { background-color: blue; }
286
+
287
+ ```
288
+
289
+
290
+
291
+ ## JavaScript
292
+
293
+ ```
294
+
295
+ (function(global, document) {
296
+
297
+
298
+
299
+ const app = (function() {
300
+
301
+
302
+
303
+ function changeClassName(value) {
304
+
305
+ return function(element) {
306
+
307
+ return function() {
308
+
309
+ element.className = value;
310
+
311
+ }
312
+
313
+ }
314
+
315
+ }
316
+
317
+
318
+
319
+ function changeText(value) {
320
+
321
+ return function(element) {
322
+
323
+ return function() {
324
+
325
+ element.textContent = value;
326
+
327
+ }
328
+
329
+ }
330
+
331
+ }
332
+
333
+
334
+
335
+ function* makeLoopGenerator(...funcs) {
336
+
337
+ while(true) {
338
+
339
+ for (let func of funcs) {
340
+
341
+ yield func();
342
+
343
+ }
344
+
345
+ }
346
+
347
+ }
348
+
349
+
350
+
351
+ function loop(generator) {
352
+
353
+ generator.next();
354
+
355
+ setTimeout(loop.bind(null, generator), 1000)
356
+
357
+ }
358
+
359
+
360
+
361
+ function init() {
362
+
363
+ const containerA = document.getElementById('containerA');
364
+
365
+ const containerB = document.getElementById('containerB');
366
+
367
+
368
+
369
+ const funcs = [
370
+
371
+ changeClassName('spring')(containerA),
372
+
373
+ changeClassName('summer')(containerA),
374
+
375
+ changeText('HELLO')(containerB),
376
+
377
+ changeClassName('autumn')(containerA),
378
+
379
+ changeClassName('winter')(containerA),
380
+
381
+ changeText('WORLD')(containerB),
382
+
383
+ ];
384
+
385
+
386
+
387
+ const generator = makeLoopGenerator(...funcs);
388
+
389
+
390
+
391
+ loop(generator)
392
+
393
+
394
+
395
+ }
396
+
397
+
398
+
399
+ return {
400
+
401
+ init: init
402
+
403
+ }
404
+
405
+
406
+
407
+ })();
408
+
409
+
410
+
411
+ document.addEventListener('DOMContentLoaded', app.init);
412
+
413
+
414
+
415
+
416
+
417
+ })(window, document);
418
+
419
+ ```
420
+
421
+
422
+
423
+ # Q3. そもそも私のPromiseの記述に誤りが無いか、もしくはもっと良い記述があればご指摘いただけると助かります
424
+
425
+
426
+
427
+ 回答に載せたコードでもエラー対応をするためのコードは省略していますが、エラーが発生する可能性があるのであれば、`new Promise()`に渡すコールバック関数内で、非同期処理が失敗した時に`reject()`が実行されるようにコードを書いてあげる必要があります。
428
+
429
+
430
+
431
+ # 補足
432
+
433
+
434
+
435
+ ※ 一応動作確認をしてからコードを載せていますが、荒く書いているのでおかしいところがあるかもしれません。ご了承ください。。。(あとで修正・補足などするかもしれません)
436
+
437
+
438
+
439
+ # 参考
440
+
441
+ 掲載コードの中で`generator`と`rest parameters`を使っていますが、ブラウザ対応状況は以下でご確認ください。(英語記事へのリンクになっていますが、時間ある時に日本語ページへリンクを変えると思います。)
442
+
443
+
444
+
445
+ - [Generator](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Generator)
446
+
447
+ - [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

test CHANGED
@@ -158,7 +158,7 @@
158
158
 
159
159
 
160
160
 
161
- function* generatorFn() {
161
+ function* makeLoopGenerator() {
162
162
 
163
163
  while(true) {
164
164
 
@@ -180,7 +180,7 @@
180
180
 
181
181
 
182
182
 
183
- runLoop(generatorFn())
183
+ runLoop(makeLoopGenerator())
184
184
 
185
185
  ```
186
186
 

6

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

2018/05/17 01:51

投稿

HayatoKamono
HayatoKamono

スコア2415

test CHANGED
@@ -1,20 +1,20 @@
1
- ![イメージ説明](8d64f96d231388f602f7a4ab286fc3ad.gif)
1
+ ![イメージ説明](33a9321d33c19a67225b30098105d4b9.gif)
2
2
 
3
- [アニメーションGifで再生](https://teratail.storage.googleapis.com/uploads/contributed_images/8d64f96d231388f602f7a4ab286fc3ad.gif)
3
+ [animationGifで再生 ](https://teratail.storage.googleapis.com/uploads/contributed_images/33a9321d33c19a67225b30098105d4b9.gif)
4
4
 
5
5
 
6
6
 
7
7
  ```
8
8
 
9
- const asyncDrink = () => {
9
+ const asyncOrderDrink = () => {
10
10
 
11
- return new Promise((resolve, reject) => {
11
+ return new Promise(resolve => {
12
12
 
13
13
  setTimeout(() => {
14
14
 
15
- console.log('drink')
15
+ console.log('コーヒーを注文する')
16
16
 
17
- resolve('drink');
17
+ resolve();
18
18
 
19
19
  }, 1000)
20
20
 
@@ -24,15 +24,15 @@
24
24
 
25
25
 
26
26
 
27
- const asyncEat = () => {
27
+ const asyncPayBill = () => {
28
28
 
29
- return new Promise((resolve, reject) => {
29
+ return new Promise(resolve => {
30
30
 
31
31
  setTimeout(() => {
32
32
 
33
- console.log('eat')
33
+ console.log('料金を支払う')
34
34
 
35
- resolve('eat');
35
+ resolve();
36
36
 
37
37
  }, 2000)
38
38
 
@@ -42,15 +42,15 @@
42
42
 
43
43
 
44
44
 
45
- const asyncSleep = () => {
45
+ const asyncDrinkCoffee = () => {
46
46
 
47
- return new Promise((resolve, reject) => {
47
+ return new Promise(resolve => {
48
48
 
49
49
  setTimeout(() => {
50
50
 
51
- console.log('sleep')
51
+ console.log('コーヒーを飲む')
52
52
 
53
- resolve('sleep');
53
+ resolve();
54
54
 
55
55
  }, 3000)
56
56
 
@@ -60,21 +60,13 @@
60
60
 
61
61
 
62
62
 
63
- const funcs = [asyncDrink, asyncEat, asyncSleep];
63
+ function* makeLoopGenerator (fns) {
64
64
 
65
+ while(true) {
65
66
 
67
+ for (var fn of fns) {
66
68
 
67
- function generatorFn(fns) {
68
-
69
- return function* () {
70
-
71
- while(true) {
72
-
73
- for (var fn of fns) {
74
-
75
- yield fn();
69
+ yield fn();
76
-
77
- }
78
70
 
79
71
  }
80
72
 
@@ -84,37 +76,33 @@
84
76
 
85
77
 
86
78
 
87
- const g = generatorFn(funcs)();
79
+ const tasks = [asyncOrderDrink, asyncPayBill, asyncDrinkCoffee];
88
80
 
89
81
 
90
82
 
91
- function loop(gen) {
83
+ const tasksGenerator = makeLoopGenerator(tasks);
92
84
 
93
- function _loop() {
94
85
 
86
+
87
+ function runLoop(generator) {
88
+
89
+ function loop() {
90
+
95
- gen.next().value.then(v => _loop());
91
+ generator.next().value.then(loop);
96
92
 
97
93
  }
98
94
 
99
-
100
-
101
- _loop();
95
+ loop();
102
96
 
103
97
  }
104
98
 
105
99
 
106
100
 
107
- loop(g)
101
+ runLoop(tasksGenerator)
108
102
 
109
103
 
110
104
 
111
105
  ```
112
-
113
-
114
-
115
- とりあえず一旦、雑なコードですが置いておきます。
116
-
117
- あとで時間のある時に修正・補足等加えると思います。
118
106
 
119
107
 
120
108
 
@@ -138,13 +126,9 @@
138
126
 
139
127
  const sleep = (delay) => {
140
128
 
141
- return new Promise((resolve, reject) => {
129
+ return new Promise((resolve) => {
142
130
 
143
- setTimeout(() => {
131
+ setTimeout(resolve, delay)
144
-
145
- resolve();
146
-
147
- }, delay)
148
132
 
149
133
  });
150
134
 
@@ -164,7 +148,7 @@
164
148
 
165
149
  function loop() {
166
150
 
167
- gen.next().value.then(v => loop());
151
+ gen.next().value.then(loop);
168
152
 
169
153
  }
170
154
 
@@ -199,3 +183,7 @@
199
183
  runLoop(generatorFn())
200
184
 
201
185
  ```
186
+
187
+
188
+
189
+ ※ 追記のコードも最初のコードのように、ループ内で実行させたい関数が可変の場合でも対応できるようにしたかったですが、時間切れの為、非対応としました。

5

a

2018/05/17 01:49

投稿

HayatoKamono
HayatoKamono

スコア2415

test CHANGED
@@ -174,7 +174,7 @@
174
174
 
175
175
 
176
176
 
177
- function* generatorFn(list) {
177
+ function* generatorFn() {
178
178
 
179
179
  while(true) {
180
180
 

4

a

2018/05/17 01:20

投稿

HayatoKamono
HayatoKamono

スコア2415

test CHANGED
@@ -180,15 +180,15 @@
180
180
 
181
181
  yield sleep(1000);
182
182
 
183
- orderCoffee('チャイティラテ');
183
+ orderCoffee('ヒーを注文する');
184
184
 
185
185
  yield sleep(2000);
186
186
 
187
- payBill('480円');
187
+ payBill('料金を支払う');
188
188
 
189
189
  yield sleep(3000);
190
190
 
191
- drinkCoffee('50ml');
191
+ drinkCoffee('コーヒーを飲む');
192
192
 
193
193
  }
194
194
 

3

a

2018/05/17 01:16

投稿

HayatoKamono
HayatoKamono

スコア2415

test CHANGED
@@ -124,11 +124,13 @@
124
124
 
125
125
 
126
126
 
127
- 質問者さんの前回の質問を見る限りでは、単に時間差で複数の同期処理を実行させたいように見受けられるので、`maisuma-kun`さんの回答にあるように`sleep`関数のようなものを作って、各同期処理の前で実行してあげるのが良さそうです。
127
+ 質問者さんの前回の質問を見る限りでは、単に時間差で複数の同期処理を実行させたいように見受けられるので、`maisumakun`さんの回答にあるように`sleep`関数のようなものを作って、各同期処理の前で実行してあげるのが良さそうです。
128
-
129
-
130
-
128
+
129
+
130
+
131
- ということで、`maisuma-kun`さんの回答を参考に以下、`generator`版のコードを追加。
131
+ ということで、`maisumakun`さんの回答を参考に以下、`generator`版のコードを追加。
132
+
133
+
132
134
 
133
135
 
134
136
 

2

a

2018/05/17 01:11

投稿

HayatoKamono
HayatoKamono

スコア2415

test CHANGED
@@ -115,3 +115,85 @@
115
115
  とりあえず一旦、雑なコードですが置いておきます。
116
116
 
117
117
  あとで時間のある時に修正・補足等加えると思います。
118
+
119
+
120
+
121
+
122
+
123
+ # 追記
124
+
125
+
126
+
127
+ 質問者さんの前回の質問を見る限りでは、単に時間差で複数の同期処理を実行させたいように見受けられるので、`maisuma-kun`さんの回答にあるように`sleep`関数のようなものを作って、各同期処理の前で実行してあげるのが良さそうです。
128
+
129
+
130
+
131
+ ということで、`maisuma-kun`さんの回答を参考に以下、`generator`版のコードを追加。
132
+
133
+
134
+
135
+ ```
136
+
137
+ const sleep = (delay) => {
138
+
139
+ return new Promise((resolve, reject) => {
140
+
141
+ setTimeout(() => {
142
+
143
+ resolve();
144
+
145
+ }, delay)
146
+
147
+ });
148
+
149
+ }
150
+
151
+
152
+
153
+ const orderCoffee = (v) => console.log(v);
154
+
155
+ const payBill = (v) => console.log(v);
156
+
157
+ const drinkCoffee = (v) => console.log(v);
158
+
159
+
160
+
161
+ function runLoop(gen) {
162
+
163
+ function loop() {
164
+
165
+ gen.next().value.then(v => loop());
166
+
167
+ }
168
+
169
+ loop();
170
+
171
+ }
172
+
173
+
174
+
175
+ function* generatorFn(list) {
176
+
177
+ while(true) {
178
+
179
+ yield sleep(1000);
180
+
181
+ orderCoffee('チャイティーラテ');
182
+
183
+ yield sleep(2000);
184
+
185
+ payBill('480円');
186
+
187
+ yield sleep(3000);
188
+
189
+ drinkCoffee('50ml');
190
+
191
+ }
192
+
193
+ }
194
+
195
+
196
+
197
+ runLoop(generatorFn())
198
+
199
+ ```

1

a

2018/05/17 01:09

投稿

HayatoKamono
HayatoKamono

スコア2415

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