-
Notifications
You must be signed in to change notification settings - Fork 0
/
12-6.html
181 lines (172 loc) · 5.37 KB
/
12-6.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
<!DOCTYPE html>
<html lang="zh-TW">
<head>
<meta charset="UTF-8" />
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<link rel="icon" href="./public/favicon.ico" />
<meta http-equiv="cache-control" content="no-cache" />
<title></title>
<link rel="stylesheet" href=" https://necolas.github.io/normalize.css/8.0.1/normalize.css" />
<link rel="stylesheet" href="./hightlight/default.min.css" />
<link rel="stylesheet" href="./css/main.css" />
<link rel="stylesheet" href="./css/copybutton.css" />
<link rel="stylesheet" href="./css/hightlight.css" />
<script src="./hightlight/hightlight.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/clipboard.js/2.0.11/clipboard.min.js"></script>
<!-- Google tag (gtag.js) -->
<script async src="https://www.googletagmanager.com/gtag/js?id=G-BEVZJDBC7Z"></script>
<script src="./js/gtag.js"></script>
</head>
<body>
<header>
<nav>
<h1>
<span id="toggle-menu"></span>
<a href="index.html"></a>
</h1>
</nav>
</header>
<main>
<aside>
<nav></nav>
</aside>
<article>
<h2 id="12-6">12-6 異步迭代 for await...of</h2>
<p>
for await...of 用於遍歷多個 Promise 是 Promise.all 的代替品,只不過 Promise. all 是 api
形式並在 then 中獲取陣列結果, 而 for await of是迴圈形式,能快速在列表中把結果取出來
</p>
<div class="article-content">
<p>
<strong>
<code>for await...of</code>
語句
</strong>
創建一個迴圈,該迴圈遍歷異步可疊代物件以及同步可疊代物件,包括:內置的
<code>String</code>
,
<code>Array</code>
,類似陣列物件 (例如
<code>arguments</code>
或
<code>NodeList</code>
),
<code>TypedArray</code>
,
<code>Map</code>
,
<code>Set</code>
和用戶定義的異步/同步疊代器。它使用物件的每個不同屬性的值調用要執行的語句來調用自定義疊代鉤子。
</p>
<p>
類似於
<code>await</code>
運算符一樣,該語句只能在一個 async function 內部使用。
</p>
<div class="notecard">
<p>
<strong>備注:</strong>
<code>for await...of</code>
不適用於不是異步可疊代的異步疊代器。
</p>
</div>
</div>
<h3>疊代異步可疊代物件</h3>
<p>你還可以疊代一個明確實現異步疊代協議的物件:</p>
<pre><code class="language-js">
var asyncIterable = {
[Symbol.asyncIterator]() {
return {
i: 0,
next() {
if (this.i < 3) {
return Promise.resolve({ value: this.i++, done: false });
}
return Promise.resolve({ done: true });
}
};
}
};
(async function() {
for await (num of asyncIterable) {
console.log(num);
}
})();
// 0
// 1
// 2
</code></pre>
<h3>疊代異步生成器</h3>
<p>
異步生成器已經實現了異步疊代器協議,所以可以用
<code>for await...of</code>
迴圈。
</p>
<pre><code class="language-js">
async function* asyncGenerator() {
var i = 0;
while (i < 3) {
yield i++;
}
}
(async function() {
for await (num of asyncGenerator()) {
console.log(num);
}
})();
// 0
// 1
// 2
</code></pre>
<p>
有關使用
<code>for await...of</code>
考慮疊代 API 中獲取數據的異步 generator
更具體的例子。這個例子首先為一個數據流創建了一個異步 generator,然後使用它來獲得這個 API
的響應值的大小。
</p>
<pre><code class="language-js">
async function* streamAsyncIterator(stream) {
const reader = stream.getReader();
try {
while (true) {
const { done, value } = await reader.read();
if (done) {
return;
}
yield value;
}
} finally {
reader.releaseLock();
}
}
// 從 url 獲取數據並使用異步 generator 來計算響應值的大小
async function getResponseSize(url) {
const response = await fetch(url);
// Will hold the size of the response, in bytes.
let responseSize = 0;
// 使用 for-await-of 迴圈。異步 generator 會遍歷響應值的每一部分
for await (const chunk of streamAsyncIterator(response.body)) {
// Incrementing the total response length.
responseSize += chunk.length;
}
console.log(`Response Size: ${responseSize} bytes`);
// expected output: "Response Size: 1071472"
return responseSize;
}
getResponseSize('https://jsonplaceholder.typicode.com/photos');
</code></pre>
<p>資料來源:</p>
<p>
<a
href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/for-await...of"
>
https://developer.mozilla.org/.../for-await...of
</a>
</p>
</article>
</main>
</body>
<script type="module" src="./js/main.js"></script>
</html>