const fetch = require("node-fetch")const sleep = (timeout) => new Promise((resolve, reject) => {setTimeout(resolve, timeout);})async function getZhihuColumn(id) {await sleep(1000)const url = `https://zhuanlan.zhihu.com/api/columns/${id}`const response = await fetch(url)return await response.json()}const showCloumnInfo = async () => {console.time('showColumnInfo')const names = [ "feweekly", "toolingtips" ];for (const namee of names) {const column = await getZhihuColumn(namee);console.log(`标题:${column.title}`)console.log(`简介:${column.intro}`)}console.timeEnd('showColumnInfo')};showCloumnInfo()
如何让代码运行更快呢?变为串行:
...const showCloumnInfo = async () => {console.time('showColumnInfo')const names = [ "feweekly", "toolingtips" ];const promises = names.map(x => getZhihuColumn(x));for (const primise of promises) {const column = await primise;console.log(`标题:${column.title}`)console.log(`简介:${column.intro}`)}console.timeEnd('showColumnInfo')};...
