質問編集履歴
3
a
test
CHANGED
File without changes
|
test
CHANGED
@@ -239,339 +239,3 @@
|
|
239
239
|
|
240
240
|
|
241
241
|
```
|
242
|
-
|
243
|
-
|
244
|
-
|
245
|
-
|
246
|
-
|
247
|
-
|
248
|
-
|
249
|
-
たったいま、以下のコードで想定通りの動きになりました。
|
250
|
-
|
251
|
-
|
252
|
-
|
253
|
-
|
254
|
-
|
255
|
-
```javascript
|
256
|
-
|
257
|
-
var process1 = function(val) {
|
258
|
-
|
259
|
-
var deferred = $q.defer();
|
260
|
-
|
261
|
-
|
262
|
-
|
263
|
-
// 仮想非同期
|
264
|
-
|
265
|
-
setTimeout(function(){
|
266
|
-
|
267
|
-
if (val) {
|
268
|
-
|
269
|
-
deferred.resolve('ok: ' + val);
|
270
|
-
|
271
|
-
} else {
|
272
|
-
|
273
|
-
deferred.reject('ng; ' + val);
|
274
|
-
|
275
|
-
}
|
276
|
-
|
277
|
-
}, 1000);
|
278
|
-
|
279
|
-
|
280
|
-
|
281
|
-
return deferred.promise;
|
282
|
-
|
283
|
-
};
|
284
|
-
|
285
|
-
|
286
|
-
|
287
|
-
var process2 = function(val) {
|
288
|
-
|
289
|
-
var deferred = $q.defer();
|
290
|
-
|
291
|
-
|
292
|
-
|
293
|
-
// 仮想非同期
|
294
|
-
|
295
|
-
setTimeout(function(){
|
296
|
-
|
297
|
-
if (val) {
|
298
|
-
|
299
|
-
deferred.resolve('ok: ' + val);
|
300
|
-
|
301
|
-
} else {
|
302
|
-
|
303
|
-
deferred.reject('ng; ' + val);
|
304
|
-
|
305
|
-
}
|
306
|
-
|
307
|
-
}, 1000);
|
308
|
-
|
309
|
-
|
310
|
-
|
311
|
-
return deferred.promise;
|
312
|
-
|
313
|
-
};
|
314
|
-
|
315
|
-
|
316
|
-
|
317
|
-
var process3 = function(val) {
|
318
|
-
|
319
|
-
var deferred = $q.defer();
|
320
|
-
|
321
|
-
|
322
|
-
|
323
|
-
// 仮想非同期
|
324
|
-
|
325
|
-
setTimeout(function(){
|
326
|
-
|
327
|
-
if (val) {
|
328
|
-
|
329
|
-
deferred.resolve('ok: ' + val);
|
330
|
-
|
331
|
-
} else {
|
332
|
-
|
333
|
-
deferred.reject('ng; ' + val);
|
334
|
-
|
335
|
-
}
|
336
|
-
|
337
|
-
}, 1000);
|
338
|
-
|
339
|
-
|
340
|
-
|
341
|
-
return deferred.promise;
|
342
|
-
|
343
|
-
};
|
344
|
-
|
345
|
-
|
346
|
-
|
347
|
-
var process3sub = function(val) {
|
348
|
-
|
349
|
-
var deferred = $q.defer();
|
350
|
-
|
351
|
-
|
352
|
-
|
353
|
-
// 仮想非同期
|
354
|
-
|
355
|
-
setTimeout(function(){
|
356
|
-
|
357
|
-
func(val, deferred);
|
358
|
-
|
359
|
-
}, 1000);
|
360
|
-
|
361
|
-
|
362
|
-
|
363
|
-
return deferred.promise;
|
364
|
-
|
365
|
-
};
|
366
|
-
|
367
|
-
|
368
|
-
|
369
|
-
var process4 = function(val) {
|
370
|
-
|
371
|
-
var deferred = $q.defer();
|
372
|
-
|
373
|
-
|
374
|
-
|
375
|
-
// 仮想非同期
|
376
|
-
|
377
|
-
setTimeout(function(){
|
378
|
-
|
379
|
-
if (val) {
|
380
|
-
|
381
|
-
deferred.resolve('ok: ' + val);
|
382
|
-
|
383
|
-
} else {
|
384
|
-
|
385
|
-
deferred.reject('ng; ' + val);
|
386
|
-
|
387
|
-
}
|
388
|
-
|
389
|
-
}, 1000);
|
390
|
-
|
391
|
-
|
392
|
-
|
393
|
-
return deferred.promise;
|
394
|
-
|
395
|
-
};
|
396
|
-
|
397
|
-
|
398
|
-
|
399
|
-
var process5 = function(val) {
|
400
|
-
|
401
|
-
var deferred = $q.defer();
|
402
|
-
|
403
|
-
|
404
|
-
|
405
|
-
// 仮想非同期
|
406
|
-
|
407
|
-
setTimeout(function(){
|
408
|
-
|
409
|
-
if (val) {
|
410
|
-
|
411
|
-
deferred.resolve('ok: ' + val);
|
412
|
-
|
413
|
-
} else {
|
414
|
-
|
415
|
-
deferred.reject('ng; ' + val);
|
416
|
-
|
417
|
-
}
|
418
|
-
|
419
|
-
}, 1000);
|
420
|
-
|
421
|
-
|
422
|
-
|
423
|
-
return deferred.promise;
|
424
|
-
|
425
|
-
};
|
426
|
-
|
427
|
-
|
428
|
-
|
429
|
-
var data=[]
|
430
|
-
|
431
|
-
process1('process1')
|
432
|
-
|
433
|
-
.then(function(val) {
|
434
|
-
|
435
|
-
// 処理
|
436
|
-
|
437
|
-
console.log('process1 done!' + val);
|
438
|
-
|
439
|
-
data.push(val);
|
440
|
-
|
441
|
-
return process2('process2')
|
442
|
-
|
443
|
-
})
|
444
|
-
|
445
|
-
.then(function(val) {
|
446
|
-
|
447
|
-
// 処理
|
448
|
-
|
449
|
-
console.log('process2 done!' + val);
|
450
|
-
|
451
|
-
data.push(val);
|
452
|
-
|
453
|
-
return process3('process3')
|
454
|
-
|
455
|
-
})
|
456
|
-
|
457
|
-
.then(function(val) {
|
458
|
-
|
459
|
-
// 処理
|
460
|
-
|
461
|
-
console.log('process3 done!' + val);
|
462
|
-
|
463
|
-
data.push(val);
|
464
|
-
|
465
|
-
return process3sub('process3sub')
|
466
|
-
|
467
|
-
})
|
468
|
-
|
469
|
-
.then(function(val) {
|
470
|
-
|
471
|
-
// 処理
|
472
|
-
|
473
|
-
console.log('process3sub done!' + val);
|
474
|
-
|
475
|
-
data.push(val);
|
476
|
-
|
477
|
-
return process4('process4')
|
478
|
-
|
479
|
-
})
|
480
|
-
|
481
|
-
.then(function(val) {
|
482
|
-
|
483
|
-
// 処理
|
484
|
-
|
485
|
-
console.log('process4 done!' + val);
|
486
|
-
|
487
|
-
data.push(val);
|
488
|
-
|
489
|
-
return process4('process5')
|
490
|
-
|
491
|
-
})
|
492
|
-
|
493
|
-
.then(function(val) {
|
494
|
-
|
495
|
-
console.log(data);
|
496
|
-
|
497
|
-
console.log('finish');
|
498
|
-
|
499
|
-
})
|
500
|
-
|
501
|
-
;
|
502
|
-
|
503
|
-
|
504
|
-
|
505
|
-
});
|
506
|
-
|
507
|
-
|
508
|
-
|
509
|
-
var res = [];
|
510
|
-
|
511
|
-
function func(val, deferred)
|
512
|
-
|
513
|
-
{
|
514
|
-
|
515
|
-
tmpVal = val;
|
516
|
-
|
517
|
-
|
518
|
-
|
519
|
-
setTimeout(function(){
|
520
|
-
|
521
|
-
console.log('sub func process');
|
522
|
-
|
523
|
-
|
524
|
-
|
525
|
-
if (val) {
|
526
|
-
|
527
|
-
res.push(val);
|
528
|
-
|
529
|
-
console.log(res);
|
530
|
-
|
531
|
-
// 適当にランダムな文字列をつくる(再起的にfuncに渡すvalを容易)
|
532
|
-
|
533
|
-
var c = 'abcdefghi';
|
534
|
-
|
535
|
-
var cl = c.length;
|
536
|
-
|
537
|
-
var r = '';
|
538
|
-
|
539
|
-
for(var i=0; i<cl; i++) {
|
540
|
-
|
541
|
-
r += c[Math.floor(Math.random()*cl)];
|
542
|
-
|
543
|
-
}
|
544
|
-
|
545
|
-
// 30%の確率で↑でつくったランダム文字列を空にする。
|
546
|
-
|
547
|
-
// (func再起関数の終了条件はvalが30%確率で空にして終了させる)
|
548
|
-
|
549
|
-
var x = Math.random();
|
550
|
-
|
551
|
-
if (x < 0.3) {
|
552
|
-
|
553
|
-
r = '';
|
554
|
-
|
555
|
-
}
|
556
|
-
|
557
|
-
console.log('will execute func() r='+r);
|
558
|
-
|
559
|
-
func(r, deferred);
|
560
|
-
|
561
|
-
} else {
|
562
|
-
|
563
|
-
console.log(res);
|
564
|
-
|
565
|
-
console.log('exit process3sub func()');
|
566
|
-
|
567
|
-
deferred.resolve('ok: process3sub');
|
568
|
-
|
569
|
-
return res;
|
570
|
-
|
571
|
-
}
|
572
|
-
|
573
|
-
}, 1000);
|
574
|
-
|
575
|
-
}
|
576
|
-
|
577
|
-
```
|
2
サンプルソース追加
test
CHANGED
File without changes
|
test
CHANGED
@@ -246,39 +246,7 @@
|
|
246
246
|
|
247
247
|
|
248
248
|
|
249
|
-
|
249
|
+
たったいま、以下のコードで想定通りの動きになりました。
|
250
|
-
|
251
|
-
この場合実行するとconsole.logが以下のような感じになり、
|
252
|
-
|
253
|
-
process3のsubプロセス(関数func())を無視してprocess4になってしまっています。
|
254
|
-
|
255
|
-
process3のsubプロセス(関数func())が終わってからprocess4を実行したいです。
|
256
|
-
|
257
|
-
|
258
|
-
|
259
|
-
process1: ok: process1
|
260
|
-
|
261
|
-
process2: ok: process2
|
262
|
-
|
263
|
-
process3: ok: process3
|
264
|
-
|
265
|
-
process4: undefined
|
266
|
-
|
267
|
-
sub func process
|
268
|
-
|
269
|
-
recursive func
|
270
|
-
|
271
|
-
Uncaught TypeError: Cannot read property 'promise' of undefinedfunc @ :372(anonymous function) @ :363
|
272
|
-
|
273
|
-
["process1", "process2", "process3", "process4"]
|
274
|
-
|
275
|
-
finish
|
276
|
-
|
277
|
-
sub func process
|
278
|
-
|
279
|
-
recursive func
|
280
|
-
|
281
|
-
|
282
250
|
|
283
251
|
|
284
252
|
|
@@ -286,322 +254,324 @@
|
|
286
254
|
|
287
255
|
```javascript
|
288
256
|
|
289
|
-
|
290
|
-
|
291
|
-
|
292
|
-
|
293
|
-
|
294
|
-
|
295
|
-
|
296
|
-
|
297
|
-
|
298
|
-
|
299
|
-
|
300
|
-
|
301
|
-
|
302
|
-
|
303
|
-
|
304
|
-
|
305
|
-
|
306
|
-
|
307
|
-
|
308
|
-
|
309
|
-
|
257
|
+
var process1 = function(val) {
|
258
|
+
|
259
|
+
var deferred = $q.defer();
|
260
|
+
|
261
|
+
|
262
|
+
|
263
|
+
// 仮想非同期
|
264
|
+
|
265
|
+
setTimeout(function(){
|
266
|
+
|
267
|
+
if (val) {
|
268
|
+
|
269
|
+
deferred.resolve('ok: ' + val);
|
270
|
+
|
271
|
+
} else {
|
272
|
+
|
273
|
+
deferred.reject('ng; ' + val);
|
274
|
+
|
275
|
+
}
|
276
|
+
|
277
|
+
}, 1000);
|
278
|
+
|
279
|
+
|
280
|
+
|
281
|
+
return deferred.promise;
|
282
|
+
|
283
|
+
};
|
284
|
+
|
285
|
+
|
286
|
+
|
287
|
+
var process2 = function(val) {
|
288
|
+
|
289
|
+
var deferred = $q.defer();
|
290
|
+
|
291
|
+
|
292
|
+
|
293
|
+
// 仮想非同期
|
294
|
+
|
295
|
+
setTimeout(function(){
|
296
|
+
|
297
|
+
if (val) {
|
298
|
+
|
299
|
+
deferred.resolve('ok: ' + val);
|
300
|
+
|
301
|
+
} else {
|
302
|
+
|
303
|
+
deferred.reject('ng; ' + val);
|
304
|
+
|
305
|
+
}
|
306
|
+
|
307
|
+
}, 1000);
|
308
|
+
|
309
|
+
|
310
|
+
|
311
|
+
return deferred.promise;
|
312
|
+
|
313
|
+
};
|
314
|
+
|
315
|
+
|
316
|
+
|
317
|
+
var process3 = function(val) {
|
318
|
+
|
319
|
+
var deferred = $q.defer();
|
320
|
+
|
321
|
+
|
322
|
+
|
323
|
+
// 仮想非同期
|
324
|
+
|
325
|
+
setTimeout(function(){
|
326
|
+
|
327
|
+
if (val) {
|
328
|
+
|
329
|
+
deferred.resolve('ok: ' + val);
|
330
|
+
|
331
|
+
} else {
|
332
|
+
|
333
|
+
deferred.reject('ng; ' + val);
|
334
|
+
|
335
|
+
}
|
336
|
+
|
337
|
+
}, 1000);
|
338
|
+
|
339
|
+
|
340
|
+
|
341
|
+
return deferred.promise;
|
342
|
+
|
343
|
+
};
|
344
|
+
|
345
|
+
|
346
|
+
|
347
|
+
var process3sub = function(val) {
|
348
|
+
|
349
|
+
var deferred = $q.defer();
|
350
|
+
|
351
|
+
|
352
|
+
|
353
|
+
// 仮想非同期
|
354
|
+
|
355
|
+
setTimeout(function(){
|
356
|
+
|
357
|
+
func(val, deferred);
|
358
|
+
|
359
|
+
}, 1000);
|
360
|
+
|
361
|
+
|
362
|
+
|
363
|
+
return deferred.promise;
|
364
|
+
|
365
|
+
};
|
366
|
+
|
367
|
+
|
368
|
+
|
369
|
+
var process4 = function(val) {
|
370
|
+
|
371
|
+
var deferred = $q.defer();
|
372
|
+
|
373
|
+
|
374
|
+
|
375
|
+
// 仮想非同期
|
376
|
+
|
377
|
+
setTimeout(function(){
|
378
|
+
|
379
|
+
if (val) {
|
380
|
+
|
381
|
+
deferred.resolve('ok: ' + val);
|
382
|
+
|
383
|
+
} else {
|
384
|
+
|
385
|
+
deferred.reject('ng; ' + val);
|
386
|
+
|
387
|
+
}
|
388
|
+
|
389
|
+
}, 1000);
|
390
|
+
|
391
|
+
|
392
|
+
|
393
|
+
return deferred.promise;
|
394
|
+
|
395
|
+
};
|
396
|
+
|
397
|
+
|
398
|
+
|
399
|
+
var process5 = function(val) {
|
400
|
+
|
401
|
+
var deferred = $q.defer();
|
402
|
+
|
403
|
+
|
404
|
+
|
405
|
+
// 仮想非同期
|
406
|
+
|
407
|
+
setTimeout(function(){
|
408
|
+
|
409
|
+
if (val) {
|
410
|
+
|
411
|
+
deferred.resolve('ok: ' + val);
|
412
|
+
|
413
|
+
} else {
|
414
|
+
|
415
|
+
deferred.reject('ng; ' + val);
|
416
|
+
|
417
|
+
}
|
418
|
+
|
419
|
+
}, 1000);
|
420
|
+
|
421
|
+
|
422
|
+
|
423
|
+
return deferred.promise;
|
424
|
+
|
425
|
+
};
|
426
|
+
|
427
|
+
|
428
|
+
|
429
|
+
var data=[]
|
430
|
+
|
431
|
+
process1('process1')
|
432
|
+
|
433
|
+
.then(function(val) {
|
434
|
+
|
435
|
+
// 処理
|
436
|
+
|
437
|
+
console.log('process1 done!' + val);
|
438
|
+
|
439
|
+
data.push(val);
|
440
|
+
|
441
|
+
return process2('process2')
|
442
|
+
|
443
|
+
})
|
444
|
+
|
445
|
+
.then(function(val) {
|
446
|
+
|
447
|
+
// 処理
|
448
|
+
|
449
|
+
console.log('process2 done!' + val);
|
450
|
+
|
451
|
+
data.push(val);
|
452
|
+
|
453
|
+
return process3('process3')
|
454
|
+
|
455
|
+
})
|
456
|
+
|
457
|
+
.then(function(val) {
|
458
|
+
|
459
|
+
// 処理
|
460
|
+
|
461
|
+
console.log('process3 done!' + val);
|
462
|
+
|
463
|
+
data.push(val);
|
464
|
+
|
465
|
+
return process3sub('process3sub')
|
466
|
+
|
467
|
+
})
|
468
|
+
|
469
|
+
.then(function(val) {
|
470
|
+
|
471
|
+
// 処理
|
472
|
+
|
473
|
+
console.log('process3sub done!' + val);
|
474
|
+
|
475
|
+
data.push(val);
|
476
|
+
|
477
|
+
return process4('process4')
|
478
|
+
|
479
|
+
})
|
480
|
+
|
481
|
+
.then(function(val) {
|
482
|
+
|
483
|
+
// 処理
|
484
|
+
|
485
|
+
console.log('process4 done!' + val);
|
486
|
+
|
487
|
+
data.push(val);
|
488
|
+
|
489
|
+
return process4('process5')
|
490
|
+
|
491
|
+
})
|
492
|
+
|
493
|
+
.then(function(val) {
|
494
|
+
|
495
|
+
console.log(data);
|
496
|
+
|
497
|
+
console.log('finish');
|
498
|
+
|
499
|
+
})
|
500
|
+
|
501
|
+
;
|
502
|
+
|
503
|
+
|
504
|
+
|
505
|
+
});
|
506
|
+
|
507
|
+
|
508
|
+
|
509
|
+
var res = [];
|
510
|
+
|
511
|
+
function func(val, deferred)
|
512
|
+
|
513
|
+
{
|
514
|
+
|
515
|
+
tmpVal = val;
|
516
|
+
|
517
|
+
|
310
518
|
|
311
519
|
setTimeout(function(){
|
312
520
|
|
521
|
+
console.log('sub func process');
|
522
|
+
|
523
|
+
|
524
|
+
|
313
525
|
if (val) {
|
314
526
|
|
527
|
+
res.push(val);
|
528
|
+
|
529
|
+
console.log(res);
|
530
|
+
|
531
|
+
// 適当にランダムな文字列をつくる(再起的にfuncに渡すvalを容易)
|
532
|
+
|
533
|
+
var c = 'abcdefghi';
|
534
|
+
|
535
|
+
var cl = c.length;
|
536
|
+
|
315
|
-
|
537
|
+
var r = '';
|
538
|
+
|
539
|
+
for(var i=0; i<cl; i++) {
|
540
|
+
|
541
|
+
r += c[Math.floor(Math.random()*cl)];
|
542
|
+
|
543
|
+
}
|
544
|
+
|
545
|
+
// 30%の確率で↑でつくったランダム文字列を空にする。
|
546
|
+
|
547
|
+
// (func再起関数の終了条件はvalが30%確率で空にして終了させる)
|
548
|
+
|
549
|
+
var x = Math.random();
|
550
|
+
|
551
|
+
if (x < 0.3) {
|
552
|
+
|
553
|
+
r = '';
|
554
|
+
|
555
|
+
}
|
556
|
+
|
557
|
+
console.log('will execute func() r='+r);
|
558
|
+
|
559
|
+
func(r, deferred);
|
316
560
|
|
317
561
|
} else {
|
318
562
|
|
563
|
+
console.log(res);
|
564
|
+
|
565
|
+
console.log('exit process3sub func()');
|
566
|
+
|
319
|
-
deferred.re
|
567
|
+
deferred.resolve('ok: process3sub');
|
568
|
+
|
569
|
+
return res;
|
320
570
|
|
321
571
|
}
|
322
572
|
|
323
573
|
}, 1000);
|
324
574
|
|
325
|
-
|
326
|
-
|
327
|
-
return deferred.promise;
|
328
|
-
|
329
|
-
};
|
330
|
-
|
331
|
-
|
332
|
-
|
333
|
-
var process2 = function(val) {
|
334
|
-
|
335
|
-
var deferred = $q.defer();
|
336
|
-
|
337
|
-
|
338
|
-
|
339
|
-
// 仮想非同期
|
340
|
-
|
341
|
-
setTimeout(function(){
|
342
|
-
|
343
|
-
if (val) {
|
344
|
-
|
345
|
-
deferred.resolve('ok: ' + val);
|
346
|
-
|
347
|
-
} else {
|
348
|
-
|
349
|
-
deferred.reject('ng; ' + val);
|
350
|
-
|
351
|
-
|
575
|
+
}
|
352
|
-
|
353
|
-
}, 1000);
|
354
|
-
|
355
|
-
|
356
|
-
|
357
|
-
return deferred.promise;
|
358
|
-
|
359
|
-
};
|
360
|
-
|
361
|
-
|
362
|
-
|
363
|
-
var process3 = function(val) {
|
364
|
-
|
365
|
-
var deferred = $q.defer();
|
366
|
-
|
367
|
-
|
368
|
-
|
369
|
-
// 仮想非同期
|
370
|
-
|
371
|
-
setTimeout(function(){
|
372
|
-
|
373
|
-
if (val) {
|
374
|
-
|
375
|
-
deferred.resolve('ok: ' + val);
|
376
|
-
|
377
|
-
} else {
|
378
|
-
|
379
|
-
deferred.reject('ng; ' + val);
|
380
|
-
|
381
|
-
}
|
382
|
-
|
383
|
-
}, 1000);
|
384
|
-
|
385
|
-
|
386
|
-
|
387
|
-
return deferred.promise;
|
388
|
-
|
389
|
-
};
|
390
|
-
|
391
|
-
|
392
|
-
|
393
|
-
var process4 = function(val) {
|
394
|
-
|
395
|
-
var deferred = $q.defer();
|
396
|
-
|
397
|
-
|
398
|
-
|
399
|
-
// 仮想非同期
|
400
|
-
|
401
|
-
setTimeout(function(){
|
402
|
-
|
403
|
-
if (val) {
|
404
|
-
|
405
|
-
deferred.resolve('ok: ' + val);
|
406
|
-
|
407
|
-
} else {
|
408
|
-
|
409
|
-
deferred.reject('ng; ' + val);
|
410
|
-
|
411
|
-
}
|
412
|
-
|
413
|
-
}, 1000);
|
414
|
-
|
415
|
-
|
416
|
-
|
417
|
-
return deferred.promise;
|
418
|
-
|
419
|
-
};
|
420
|
-
|
421
|
-
|
422
|
-
|
423
|
-
var process5 = function(val) {
|
424
|
-
|
425
|
-
var deferred = $q.defer();
|
426
|
-
|
427
|
-
|
428
|
-
|
429
|
-
// 仮想非同期
|
430
|
-
|
431
|
-
setTimeout(function(){
|
432
|
-
|
433
|
-
if (val) {
|
434
|
-
|
435
|
-
deferred.resolve('ok: ' + val);
|
436
|
-
|
437
|
-
} else {
|
438
|
-
|
439
|
-
deferred.reject('ng; ' + val);
|
440
|
-
|
441
|
-
}
|
442
|
-
|
443
|
-
}, 1000);
|
444
|
-
|
445
|
-
|
446
|
-
|
447
|
-
return deferred.promise;
|
448
|
-
|
449
|
-
};
|
450
|
-
|
451
|
-
|
452
|
-
|
453
|
-
var data=[]
|
454
|
-
|
455
|
-
process1('process1')
|
456
|
-
|
457
|
-
.then(function(val) {
|
458
|
-
|
459
|
-
// 処理
|
460
|
-
|
461
|
-
console.log('process1: ' + val);
|
462
|
-
|
463
|
-
data.push('process1');
|
464
|
-
|
465
|
-
return process2('process2')
|
466
|
-
|
467
|
-
})
|
468
|
-
|
469
|
-
.then(function(val) {
|
470
|
-
|
471
|
-
// 処理
|
472
|
-
|
473
|
-
console.log('process2: ' + val);
|
474
|
-
|
475
|
-
data.push('process2');
|
476
|
-
|
477
|
-
return process3('process3')
|
478
|
-
|
479
|
-
})
|
480
|
-
|
481
|
-
.then(function(val) {
|
482
|
-
|
483
|
-
// 処理
|
484
|
-
|
485
|
-
console.log('process3: ' + val);
|
486
|
-
|
487
|
-
data.push('process3');
|
488
|
-
|
489
|
-
|
490
|
-
|
491
|
-
var deferred = $q.defer();
|
492
|
-
|
493
|
-
|
494
|
-
|
495
|
-
func(val, deferred)
|
496
|
-
|
497
|
-
.then(function(val) {
|
498
|
-
|
499
|
-
console.log('callback sub func val: '+val);
|
500
|
-
|
501
|
-
return process4('process4')
|
502
|
-
|
503
|
-
});
|
504
|
-
|
505
|
-
})
|
506
|
-
|
507
|
-
.then(function(val) {
|
508
|
-
|
509
|
-
// 処理
|
510
|
-
|
511
|
-
console.log('process4: ' + val);
|
512
|
-
|
513
|
-
data.push('process4');
|
514
|
-
|
515
|
-
return process4('process5')
|
516
|
-
|
517
|
-
})
|
518
|
-
|
519
|
-
.then(function(val) {
|
520
|
-
|
521
|
-
console.log(data);
|
522
|
-
|
523
|
-
console.log('finish');
|
524
|
-
|
525
|
-
})
|
526
|
-
|
527
|
-
;
|
528
|
-
|
529
|
-
|
530
|
-
|
531
|
-
});
|
532
|
-
|
533
|
-
|
534
|
-
|
535
|
-
var res = [];
|
536
|
-
|
537
|
-
function func(val, deferred)
|
538
|
-
|
539
|
-
{
|
540
|
-
|
541
|
-
tmpVal = val;
|
542
|
-
|
543
|
-
|
544
|
-
|
545
|
-
setTimeout(function(){
|
546
|
-
|
547
|
-
console.log('sub func process');
|
548
|
-
|
549
|
-
|
550
|
-
|
551
|
-
if (val) {
|
552
|
-
|
553
|
-
res.push(val);
|
554
|
-
|
555
|
-
var c = 'abcdefghi';
|
556
|
-
|
557
|
-
var cl = c.length;
|
558
|
-
|
559
|
-
var r = '';
|
560
|
-
|
561
|
-
for(var i=0; i<cl; i++) {
|
562
|
-
|
563
|
-
r += c[Math.floor(Math.random()*cl)];
|
564
|
-
|
565
|
-
}
|
566
|
-
|
567
|
-
var x = Math.random();
|
568
|
-
|
569
|
-
if (x < 0.3) {
|
570
|
-
|
571
|
-
r = '';
|
572
|
-
|
573
|
-
}
|
574
|
-
|
575
|
-
console.log('recursive func');
|
576
|
-
|
577
|
-
func(r);
|
578
|
-
|
579
|
-
} else {
|
580
|
-
|
581
|
-
console.log(res);
|
582
|
-
|
583
|
-
console.log('exit sub func process');
|
584
|
-
|
585
|
-
deferred.resolve('ok: sub func process');
|
586
|
-
|
587
|
-
return res;
|
588
|
-
|
589
|
-
}
|
590
|
-
|
591
|
-
}, 1000);
|
592
|
-
|
593
|
-
|
594
|
-
|
595
|
-
return deferred.promise;
|
596
|
-
|
597
|
-
}
|
598
|
-
|
599
|
-
|
600
|
-
|
601
|
-
}());
|
602
|
-
|
603
|
-
|
604
|
-
|
605
|
-
</script>
|
606
576
|
|
607
577
|
```
|
1
サンプルソース更新
test
CHANGED
File without changes
|
test
CHANGED
@@ -34,6 +34,8 @@
|
|
34
34
|
|
35
35
|
```javascript
|
36
36
|
|
37
|
+
|
38
|
+
|
37
39
|
var process1 = function(val) {
|
38
40
|
|
39
41
|
var deferred = $q.defer();
|
@@ -237,3 +239,369 @@
|
|
237
239
|
|
238
240
|
|
239
241
|
```
|
242
|
+
|
243
|
+
|
244
|
+
|
245
|
+
|
246
|
+
|
247
|
+
|
248
|
+
|
249
|
+
上記から調査して、以下のソースになっているのですが、
|
250
|
+
|
251
|
+
この場合実行するとconsole.logが以下のような感じになり、
|
252
|
+
|
253
|
+
process3のsubプロセス(関数func())を無視してprocess4になってしまっています。
|
254
|
+
|
255
|
+
process3のsubプロセス(関数func())が終わってからprocess4を実行したいです。
|
256
|
+
|
257
|
+
|
258
|
+
|
259
|
+
process1: ok: process1
|
260
|
+
|
261
|
+
process2: ok: process2
|
262
|
+
|
263
|
+
process3: ok: process3
|
264
|
+
|
265
|
+
process4: undefined
|
266
|
+
|
267
|
+
sub func process
|
268
|
+
|
269
|
+
recursive func
|
270
|
+
|
271
|
+
Uncaught TypeError: Cannot read property 'promise' of undefinedfunc @ :372(anonymous function) @ :363
|
272
|
+
|
273
|
+
["process1", "process2", "process3", "process4"]
|
274
|
+
|
275
|
+
finish
|
276
|
+
|
277
|
+
sub func process
|
278
|
+
|
279
|
+
recursive func
|
280
|
+
|
281
|
+
|
282
|
+
|
283
|
+
|
284
|
+
|
285
|
+
|
286
|
+
|
287
|
+
```javascript
|
288
|
+
|
289
|
+
<script type="text/javascript">
|
290
|
+
|
291
|
+
|
292
|
+
|
293
|
+
(function () {
|
294
|
+
|
295
|
+
angular.module('appName', [], function() {});
|
296
|
+
|
297
|
+
angular.module('appName').controller('MainCtrl', function($scope, $q, $http) {
|
298
|
+
|
299
|
+
|
300
|
+
|
301
|
+
|
302
|
+
|
303
|
+
var process1 = function(val) {
|
304
|
+
|
305
|
+
var deferred = $q.defer();
|
306
|
+
|
307
|
+
|
308
|
+
|
309
|
+
// 仮想非同期
|
310
|
+
|
311
|
+
setTimeout(function(){
|
312
|
+
|
313
|
+
if (val) {
|
314
|
+
|
315
|
+
deferred.resolve('ok: ' + val);
|
316
|
+
|
317
|
+
} else {
|
318
|
+
|
319
|
+
deferred.reject('ng; ' + val);
|
320
|
+
|
321
|
+
}
|
322
|
+
|
323
|
+
}, 1000);
|
324
|
+
|
325
|
+
|
326
|
+
|
327
|
+
return deferred.promise;
|
328
|
+
|
329
|
+
};
|
330
|
+
|
331
|
+
|
332
|
+
|
333
|
+
var process2 = function(val) {
|
334
|
+
|
335
|
+
var deferred = $q.defer();
|
336
|
+
|
337
|
+
|
338
|
+
|
339
|
+
// 仮想非同期
|
340
|
+
|
341
|
+
setTimeout(function(){
|
342
|
+
|
343
|
+
if (val) {
|
344
|
+
|
345
|
+
deferred.resolve('ok: ' + val);
|
346
|
+
|
347
|
+
} else {
|
348
|
+
|
349
|
+
deferred.reject('ng; ' + val);
|
350
|
+
|
351
|
+
}
|
352
|
+
|
353
|
+
}, 1000);
|
354
|
+
|
355
|
+
|
356
|
+
|
357
|
+
return deferred.promise;
|
358
|
+
|
359
|
+
};
|
360
|
+
|
361
|
+
|
362
|
+
|
363
|
+
var process3 = function(val) {
|
364
|
+
|
365
|
+
var deferred = $q.defer();
|
366
|
+
|
367
|
+
|
368
|
+
|
369
|
+
// 仮想非同期
|
370
|
+
|
371
|
+
setTimeout(function(){
|
372
|
+
|
373
|
+
if (val) {
|
374
|
+
|
375
|
+
deferred.resolve('ok: ' + val);
|
376
|
+
|
377
|
+
} else {
|
378
|
+
|
379
|
+
deferred.reject('ng; ' + val);
|
380
|
+
|
381
|
+
}
|
382
|
+
|
383
|
+
}, 1000);
|
384
|
+
|
385
|
+
|
386
|
+
|
387
|
+
return deferred.promise;
|
388
|
+
|
389
|
+
};
|
390
|
+
|
391
|
+
|
392
|
+
|
393
|
+
var process4 = function(val) {
|
394
|
+
|
395
|
+
var deferred = $q.defer();
|
396
|
+
|
397
|
+
|
398
|
+
|
399
|
+
// 仮想非同期
|
400
|
+
|
401
|
+
setTimeout(function(){
|
402
|
+
|
403
|
+
if (val) {
|
404
|
+
|
405
|
+
deferred.resolve('ok: ' + val);
|
406
|
+
|
407
|
+
} else {
|
408
|
+
|
409
|
+
deferred.reject('ng; ' + val);
|
410
|
+
|
411
|
+
}
|
412
|
+
|
413
|
+
}, 1000);
|
414
|
+
|
415
|
+
|
416
|
+
|
417
|
+
return deferred.promise;
|
418
|
+
|
419
|
+
};
|
420
|
+
|
421
|
+
|
422
|
+
|
423
|
+
var process5 = function(val) {
|
424
|
+
|
425
|
+
var deferred = $q.defer();
|
426
|
+
|
427
|
+
|
428
|
+
|
429
|
+
// 仮想非同期
|
430
|
+
|
431
|
+
setTimeout(function(){
|
432
|
+
|
433
|
+
if (val) {
|
434
|
+
|
435
|
+
deferred.resolve('ok: ' + val);
|
436
|
+
|
437
|
+
} else {
|
438
|
+
|
439
|
+
deferred.reject('ng; ' + val);
|
440
|
+
|
441
|
+
}
|
442
|
+
|
443
|
+
}, 1000);
|
444
|
+
|
445
|
+
|
446
|
+
|
447
|
+
return deferred.promise;
|
448
|
+
|
449
|
+
};
|
450
|
+
|
451
|
+
|
452
|
+
|
453
|
+
var data=[]
|
454
|
+
|
455
|
+
process1('process1')
|
456
|
+
|
457
|
+
.then(function(val) {
|
458
|
+
|
459
|
+
// 処理
|
460
|
+
|
461
|
+
console.log('process1: ' + val);
|
462
|
+
|
463
|
+
data.push('process1');
|
464
|
+
|
465
|
+
return process2('process2')
|
466
|
+
|
467
|
+
})
|
468
|
+
|
469
|
+
.then(function(val) {
|
470
|
+
|
471
|
+
// 処理
|
472
|
+
|
473
|
+
console.log('process2: ' + val);
|
474
|
+
|
475
|
+
data.push('process2');
|
476
|
+
|
477
|
+
return process3('process3')
|
478
|
+
|
479
|
+
})
|
480
|
+
|
481
|
+
.then(function(val) {
|
482
|
+
|
483
|
+
// 処理
|
484
|
+
|
485
|
+
console.log('process3: ' + val);
|
486
|
+
|
487
|
+
data.push('process3');
|
488
|
+
|
489
|
+
|
490
|
+
|
491
|
+
var deferred = $q.defer();
|
492
|
+
|
493
|
+
|
494
|
+
|
495
|
+
func(val, deferred)
|
496
|
+
|
497
|
+
.then(function(val) {
|
498
|
+
|
499
|
+
console.log('callback sub func val: '+val);
|
500
|
+
|
501
|
+
return process4('process4')
|
502
|
+
|
503
|
+
});
|
504
|
+
|
505
|
+
})
|
506
|
+
|
507
|
+
.then(function(val) {
|
508
|
+
|
509
|
+
// 処理
|
510
|
+
|
511
|
+
console.log('process4: ' + val);
|
512
|
+
|
513
|
+
data.push('process4');
|
514
|
+
|
515
|
+
return process4('process5')
|
516
|
+
|
517
|
+
})
|
518
|
+
|
519
|
+
.then(function(val) {
|
520
|
+
|
521
|
+
console.log(data);
|
522
|
+
|
523
|
+
console.log('finish');
|
524
|
+
|
525
|
+
})
|
526
|
+
|
527
|
+
;
|
528
|
+
|
529
|
+
|
530
|
+
|
531
|
+
});
|
532
|
+
|
533
|
+
|
534
|
+
|
535
|
+
var res = [];
|
536
|
+
|
537
|
+
function func(val, deferred)
|
538
|
+
|
539
|
+
{
|
540
|
+
|
541
|
+
tmpVal = val;
|
542
|
+
|
543
|
+
|
544
|
+
|
545
|
+
setTimeout(function(){
|
546
|
+
|
547
|
+
console.log('sub func process');
|
548
|
+
|
549
|
+
|
550
|
+
|
551
|
+
if (val) {
|
552
|
+
|
553
|
+
res.push(val);
|
554
|
+
|
555
|
+
var c = 'abcdefghi';
|
556
|
+
|
557
|
+
var cl = c.length;
|
558
|
+
|
559
|
+
var r = '';
|
560
|
+
|
561
|
+
for(var i=0; i<cl; i++) {
|
562
|
+
|
563
|
+
r += c[Math.floor(Math.random()*cl)];
|
564
|
+
|
565
|
+
}
|
566
|
+
|
567
|
+
var x = Math.random();
|
568
|
+
|
569
|
+
if (x < 0.3) {
|
570
|
+
|
571
|
+
r = '';
|
572
|
+
|
573
|
+
}
|
574
|
+
|
575
|
+
console.log('recursive func');
|
576
|
+
|
577
|
+
func(r);
|
578
|
+
|
579
|
+
} else {
|
580
|
+
|
581
|
+
console.log(res);
|
582
|
+
|
583
|
+
console.log('exit sub func process');
|
584
|
+
|
585
|
+
deferred.resolve('ok: sub func process');
|
586
|
+
|
587
|
+
return res;
|
588
|
+
|
589
|
+
}
|
590
|
+
|
591
|
+
}, 1000);
|
592
|
+
|
593
|
+
|
594
|
+
|
595
|
+
return deferred.promise;
|
596
|
+
|
597
|
+
}
|
598
|
+
|
599
|
+
|
600
|
+
|
601
|
+
}());
|
602
|
+
|
603
|
+
|
604
|
+
|
605
|
+
</script>
|
606
|
+
|
607
|
+
```
|