回答編集履歴
1
変な所なおした
test
CHANGED
@@ -61,7 +61,10 @@
|
|
61
61
|
})
|
62
62
|
});
|
63
63
|
|
64
|
+
// [url1, url2, url3]にしてfor文で非同期処理を書けるようにして?
|
65
|
+
// いやーきついでしょ。
|
66
|
+
|
64
|
-
// Promise.thenを使う
|
67
|
+
// Promise.thenを使うとコードのネストが1段階より先にいかない
|
65
68
|
Promise.resolve([])
|
66
69
|
.then(icons => getIcons(url1).then(value => [...icons, value]))
|
67
70
|
.then(icons => getIcons(url2).then(value => [...icons, value]))
|
@@ -69,6 +72,17 @@
|
|
69
72
|
.then(icons => {
|
70
73
|
console.log(icons);
|
71
74
|
});
|
75
|
+
|
76
|
+
// Promiseの登場により可変回数の非同期処理も簡単に記述出来るようになった
|
77
|
+
let promise = Promise.resolve([]);
|
78
|
+
for (const url of [url1, url2, url3]) {
|
79
|
+
promise = promise.then(icons =>
|
80
|
+
getIcon(url).then(value => [...icons, value])
|
81
|
+
);
|
82
|
+
}
|
83
|
+
promise.then(icons => {
|
84
|
+
console.log(icons);
|
85
|
+
});
|
72
86
|
```
|
73
87
|
|
74
88
|
逆に言うとPromiseはこの`.then(fn)`メソッドを数珠つなぎにして
|
@@ -157,7 +171,7 @@
|
|
157
171
|
// もうこれでよくね?
|
158
172
|
const main4 = async () => {
|
159
173
|
const icons = [];
|
160
|
-
for ({link, icon} of filteredResults) {
|
174
|
+
for (const {link, icon} of filteredResults) {
|
161
175
|
const value = await getIcon(link);
|
162
176
|
icons.push({...value, icon});
|
163
177
|
}
|