Skip to content

Commit b7652ea

Browse files
committed
lession 08: await in loops
1 parent ca5c5a7 commit b7652ea

File tree

1 file changed

+25
-0
lines changed

1 file changed

+25
-0
lines changed

08-await-in-loops.js

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
const fetch = require('node-fetch');
2+
const bluebird = require('bluebird');
3+
4+
async function getZhihuColumn(id) {
5+
await bluebird.delay(1000);
6+
const url = `https://zhuanlan.zhihu.com/api/columns/${id}`;
7+
const response = await fetch(url);
8+
return await response.json();
9+
}
10+
11+
const showColumnInfo = async () => {
12+
console.time('showColumnInfo');
13+
14+
const names = ['feweekly', 'toolingtips'];
15+
const promises = names.map(x => getZhihuColumn(x));
16+
for (const promise of promises) {
17+
const column = await promise;
18+
console.log(`Name: ${column.name}`);
19+
console.log(`Intro: ${column.intro}`);
20+
}
21+
22+
console.timeEnd('showColumnInfo');
23+
};
24+
25+
showColumnInfo();

0 commit comments

Comments
 (0)