1 Star 0 Fork 0

level/async

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
6使用Promise.all()让多个await操作并行.js 754 Bytes
一键复制 编辑 原始数据 按行查看 历史
baikaifa 提交于 2019-09-28 18:50 . async-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 () => {
console.time("showColumnInfo");
const [feweekly, toolingtips] = await Promise.all([
getZhihuColumn("fewwekly"),
getZhihuColumn("toolingtips")
]);
console.log(`NAME:${feweekly.name}`);
console.log(`INTEO:${feweekly.intro}`);
console.log(`NAME:${toolingtips.name}`);
console.log(`INTRO:${toolingtips.intro}`);
console.timeEnd("showColumnInfo");
};
showColumnInfo();
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/keiferr/async.git
[email protected]:keiferr/async.git
keiferr
async
async
master

搜索帮助