箭头函数

  1. const getZhihuColumn = async (id) => {
  2. const url = `https://zhuanlan.zhihu.com/api/columns/${id}`
  3. const response = await fetch(url)
  4. return await response.json()
  5. }

立即执行函数

  1. (async () => {
  2. const column = await getZhihuColumn("feweekly")
  3. console.log(`标题:${column.title}`)
  4. console.log(`简介:${column.intro}`)
  5. })();

class 类

  1. class APIClient {
  2. async getColumn (id) {
  3. const url = `https://zhuanlan.zhihu.com/api/columns/${id}`
  4. const response = await fetch(url)
  5. return await response.json()
  6. }
  7. }

整体代码

  1. const fetch = require("node-fetch")
  2. const getZhihuColumn = async (id) => {
  3. const url = `https://zhuanlan.zhihu.com/api/columns/${id}`
  4. const response = await fetch(url)
  5. return await response.json()
  6. }
  7. class APIClient {
  8. async getColumn (id) {
  9. const url = `https://zhuanlan.zhihu.com/api/columns/${id}`
  10. const response = await fetch(url)
  11. return await response.json()
  12. }
  13. }
  14. (async () => {
  15. const client = await new APIClient()
  16. const column = await getZhihuColumn("feweekly")
  17. // const column = await client.getColumn("feweekly")
  18. console.log(`标题:${column.title}`)
  19. console.log(`简介:${column.intro}`)
  20. })();