回答編集履歴
2
追記
test
CHANGED
@@ -17,3 +17,59 @@
|
|
17
17
|
});
|
18
18
|
});
|
19
19
|
```
|
20
|
+
# (参考)error処理
|
21
|
+
わざとエラーを返すsample2.php
|
22
|
+
```PHP
|
23
|
+
<?PHP
|
24
|
+
if($_GET["x"]=="3"){
|
25
|
+
header('HTTP/1.1 403 Forbidden');
|
26
|
+
exit;
|
27
|
+
}
|
28
|
+
if($_GET["x"]=="5"){
|
29
|
+
header('HTTP/1.1 500 Internal Server Error');
|
30
|
+
exit;
|
31
|
+
}
|
32
|
+
print "res".$_GET["x"];
|
33
|
+
```
|
34
|
+
(1)エラーを無視
|
35
|
+
```javascript
|
36
|
+
window.addEventListener('DOMContentLoaded', ()=>{
|
37
|
+
const n=10;
|
38
|
+
const url="sample2.php";
|
39
|
+
const urls=Array(n).fill(null).map((_,x)=>`${url}?x=${x}`);
|
40
|
+
Promise.all(urls.map(x=>fetch(x).then(res=>res.text()))).then(res=>{
|
41
|
+
console.log(res);
|
42
|
+
});
|
43
|
+
});
|
44
|
+
```
|
45
|
+
(2)エラーを含むデータを取得
|
46
|
+
```javascript
|
47
|
+
window.addEventListener('DOMContentLoaded', ()=>{
|
48
|
+
const n=10;
|
49
|
+
const url="sample2.php";
|
50
|
+
const urls=Array(n).fill(null).map((_,x)=>`${url}?x=${x}`);
|
51
|
+
Promise.all(urls.map(x=>fetch(x).then(res=>{
|
52
|
+
if(res.ok) return res.text();
|
53
|
+
throw new Error(res.status);
|
54
|
+
}).catch(err=>err))).then(res=>{
|
55
|
+
console.log(res);
|
56
|
+
});
|
57
|
+
});
|
58
|
+
```
|
59
|
+
(3)エラーを取得したら止める
|
60
|
+
```javascript
|
61
|
+
window.addEventListener('DOMContentLoaded', ()=>{
|
62
|
+
const n=10;
|
63
|
+
const url="sample2.php";
|
64
|
+
const urls=Array(n).fill(null).map((_,x)=>`${url}?x=${x}`);
|
65
|
+
Promise.all(urls.map(x=>fetch(x).then(res=>{
|
66
|
+
if(res.ok) return res.text();
|
67
|
+
throw new Error(res.status);
|
68
|
+
}))).then(res=>{
|
69
|
+
console.log(res);
|
70
|
+
}).catch(err=>{
|
71
|
+
console.log(err);
|
72
|
+
});
|
73
|
+
});
|
74
|
+
```
|
75
|
+
|
1
調整
test
CHANGED
@@ -1,5 +1,5 @@
|
|
1
1
|
なんとも言えませんが、要求量というよりはurlがおかしい箇所があったりするのでは?
|
2
|
-
fetchでテストする限り1000個くらいなげても特に問題はなさそうです。
|
2
|
+
fetchでテストする限り1000個くらいなげてもPromiseには特に問題はなさそうです。
|
3
3
|
(自前サーバーでなければスパム攻撃とみなされそうな気がしますが)
|
4
4
|
sample.php
|
5
5
|
```PHP
|