1. const fetch = require("node-fetch")
    2. const sleep = (timeout) => new Promise((resolve, reject) => {
    3. setTimeout(resolve, timeout);
    4. })
    5. async function getZhihuColumn(id) {
    6. await sleep(1000)
    7. const url = `https://zhuanlan.zhihu.com/api/columns/${id}`
    8. const response = await fetch(url)
    9. return await response.json()
    10. }
    11. const showCloumnInfo = async () => {
    12. console.time('showColumnInfo')
    13. const names = [ "feweekly", "toolingtips" ];
    14. for (const namee of names) {
    15. const column = await getZhihuColumn(namee);
    16. console.log(`标题:${column.title}`)
    17. console.log(`简介:${column.intro}`)
    18. }
    19. console.timeEnd('showColumnInfo')
    20. };
    21. showCloumnInfo()

    如何让代码运行更快呢?变为串行:

    1. ...
    2. const showCloumnInfo = async () => {
    3. console.time('showColumnInfo')
    4. const names = [ "feweekly", "toolingtips" ];
    5. const promises = names.map(x => getZhihuColumn(x));
    6. for (const primise of promises) {
    7. const column = await primise;
    8. console.log(`标题:${column.title}`)
    9. console.log(`简介:${column.intro}`)
    10. }
    11. console.timeEnd('showColumnInfo')
    12. };
    13. ...