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

回答編集履歴

1

追記

2019/01/08 03:42

投稿

yambejp
yambejp

スコア117906

answer CHANGED
@@ -12,7 +12,7 @@
12
12
 
13
13
  ```
14
14
  ajaxのthenの中で、promiseで順番を守って下さい
15
-
15
+ (追記:よく考えたらその必要はない)
16
16
  ```javascript
17
17
  $(function(){
18
18
  $.ajax({
@@ -24,4 +24,24 @@
24
24
  });
25
25
  });
26
26
 
27
+ ```
28
+
29
+ # 追記
30
+
31
+ thenで連続してつなぐならこう書けますね
32
+
33
+ ```javascript
34
+ $(function(){
35
+ var d=Array(3).fill(null).map(function(){return $.Deferred()});
36
+ $.ajax({
37
+ }).then(function(){
38
+ return d[0].promise(setTimeout(function(){console.log(1);d[0].resolve();},300));
39
+ }).then(function(){
40
+ return d[1].promise(setTimeout(function(){console.log(2);d[1].resolve();},200));
41
+ }).then(function(){
42
+ return d[2].promise(setTimeout(function(){console.log(3);d[2].resolve();},100));
43
+ }).then(function(){
44
+ setTimeout(function(){console.log(4);},100);//最後の処理はpromise不要
45
+ });
46
+ });
27
47
  ```