ES Next & Arrow function & Promise & Iterator & Generator yield & Async Await
2024-09-06 05:59:40
ES Next & Arrow function & Promise & Iterator & Generator yield & Async Await
const fetchJSON = (url = ``) => {
return fetch(url,
{
method: "GET",
// mode: "no-cors",
mode: "cors",
credentials: "same-origin",
headers: {
"Content-Type": "application/json; charset=utf-8",
},
})
.then(res => res.json())
.then(
(json) => {
return json;
}
)
.catch(err => console.log(`fetch error`, err));
};
// async / await
async function getDatas(url = ``) {
try {
return await fetchJSON(url);
} catch (err) {
console.error("getDatas error:\n", err);
}
}
// demo
getDatas(`https://cdn.xgqfrms.xyz/json/ssr/posts.json`);
Iterator & Generator yield
迭代器,生成器
https://developer.mozilla.org/zh-CN/docs/Web/JavaScript/Guide/Iterators_and_Generators
function* idMaker(ids = []) {
let len = ids.length;
if(len) {
for(let i = 0; i < len; i++) {
yield i;
}
} else {
yield undefined;
}
}
let gen = idMaker([0, 1, 2]);
gen.next();
// {value: 0, done: false}
gen.next();
// {value: 1, done: false}
gen.next();
// {value: 2, done: false}
gen.next();
// {value: undefined, done: true}
xgqfrms 2012-2020
www.cnblogs.com 发布文章使用:只允许注册用户才可以访问!
最新文章
- Atitit 视频编码与动画原理attilax总结
- X5的UI部分和传统Web页面开发的差异
- BrandZ:2016年全球最具价值品牌百强榜(完整报告)
- [转载]SharePoint 网站管理-PowerShell
- jquery.ajax和Ajax 获取数据
- layout_weight属性详解
- BZOJ_3940_[Usaco2015 Feb]Censoring_AC自动机
- wait event &; wake up
- ldap配置系列二:jenkins集成ldap
- Servlet CDI example analysis
- pytest--fixture参数化的实现方式和执行顺序
- 20170506计划-----(基于python查询oracle语句)
- vue(一)使用vue-cli搭建项目
- docker运行php网站程序
- Python_冒泡排序
- apache、nginx、tomcat配置方法
- Jquery 组 checkbox全选按钮
- nmap扫描内网存活机器脚本
- Spring Batch 简介
- spring ioc和aop的理解
热门文章
- VMware 虚拟机逃逸漏洞
- windows IOCP 实践
- TCMalloc源码学习(二)
- day133:2RenMJ:TypeScript的变量&;函数&;类&;接口
- hbase伪分布式环境的搭建
- 31-1.解决service iptables save出错
- java中== equal hashcode 比较的区别
- (EX)中国剩余定理
- Atcoder Educational DP Contest I - Coins (概率DP)
- PowerShell启用多跳远程控制