回答編集履歴
1
ちょうせい
test
CHANGED
@@ -13,3 +13,57 @@
|
|
13
13
|
})();
|
14
14
|
|
15
15
|
```
|
16
|
+
|
17
|
+
|
18
|
+
|
19
|
+
# 参考
|
20
|
+
|
21
|
+
ご提示のソースを活かす場合
|
22
|
+
|
23
|
+
- Loadtext()をasyncにする
|
24
|
+
|
25
|
+
- xhrをreturnする
|
26
|
+
|
27
|
+
|
28
|
+
|
29
|
+
の2点工夫が必要です。
|
30
|
+
|
31
|
+
その上でLoadtext()のthenで戻り値を受けます
|
32
|
+
|
33
|
+
あえてsentencesに受ける必要はありません
|
34
|
+
|
35
|
+
どうしても受ける場合はfetchの例にあるようなasnyc/awaitで処理が必要です
|
36
|
+
|
37
|
+
```javascript
|
38
|
+
|
39
|
+
async function Loadtext() {
|
40
|
+
|
41
|
+
var xhr = new XMLHttpRequest();
|
42
|
+
|
43
|
+
xhr.open("GET", text, true);
|
44
|
+
|
45
|
+
xhr.onreadystatechange = function () {
|
46
|
+
|
47
|
+
if(xhr.readyState === 4 && xhr.status === 200) {
|
48
|
+
|
49
|
+
console.log(xhr.responseText);//きちんとテキストが出力される。
|
50
|
+
|
51
|
+
console.log(typeof xhr.responseText);//きちんとstrになっている。
|
52
|
+
|
53
|
+
return xhr.responseText;
|
54
|
+
|
55
|
+
}
|
56
|
+
|
57
|
+
};
|
58
|
+
|
59
|
+
xhr.send();
|
60
|
+
|
61
|
+
return xhr;
|
62
|
+
|
63
|
+
}
|
64
|
+
|
65
|
+
Loadtext().then(res=>typeof res.responseText).then(console.log);
|
66
|
+
|
67
|
+
|
68
|
+
|
69
|
+
```
|