您的位置:首页 > 大数据 > 人工智能

async/await - 6.使用Promise.all()让多个await操作并行

2017-11-11 22:42 621 查看

async/await - 6.使用Promise.all()让多个await操作并行

const fetch = require('node-fetch')

const sleep = (timeout = 2000) => new Promise(resolve => {
setTimeout(resolve, timeout)
})

async function getZhihuColumn(id) {
await sleep(2000)
const url = `https://zhuanlan.zhihu.com/api/columns/${id}`
const response = await fetch(url)
return await response.json()
}

const showColumnInfo = async (id) => {
console.time('showColumnInfo')
const [qianduanzhidian, FrontendMagazine] = await Promise.all([
getZhihuColumn('qianduanzhidian'),
getZhihuColumn('FrontendMagazine')
])

console.log(`name:${qianduanzhidian.name}`)
console.log(`description:${qianduanzhidian.description}`)

console.log(`name:${FrontendMagazine.name}`)
console.log(`description:${FrontendMagazine.description}`)

console.timeEnd('showColumnInfo')
}
showColumnInfo() // 2630.869ms
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: