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 发布文章使用:只允许注册用户才可以访问!


最新文章

  1. Atitit 视频编码与动画原理attilax总结
  2. X5的UI部分和传统Web页面开发的差异
  3. BrandZ:2016年全球最具价值品牌百强榜(完整报告)
  4. [转载]SharePoint 网站管理-PowerShell
  5. jquery.ajax和Ajax 获取数据
  6. layout_weight属性详解
  7. BZOJ_3940_[Usaco2015 Feb]Censoring_AC自动机
  8. wait event &amp; wake up
  9. ldap配置系列二:jenkins集成ldap
  10. Servlet CDI example analysis
  11. pytest--fixture参数化的实现方式和执行顺序
  12. 20170506计划-----(基于python查询oracle语句)
  13. vue(一)使用vue-cli搭建项目
  14. docker运行php网站程序
  15. Python_冒泡排序
  16. apache、nginx、tomcat配置方法
  17. Jquery 组 checkbox全选按钮
  18. nmap扫描内网存活机器脚本
  19. Spring Batch 简介
  20. spring ioc和aop的理解

热门文章

  1. VMware 虚拟机逃逸漏洞
  2. windows IOCP 实践
  3. TCMalloc源码学习(二)
  4. day133:2RenMJ:TypeScript的变量&amp;函数&amp;类&amp;接口
  5. hbase伪分布式环境的搭建
  6. 31-1.解决service iptables save出错
  7. java中== equal hashcode 比较的区别
  8. (EX)中国剩余定理
  9. Atcoder Educational DP Contest I - Coins (概率DP)
  10. PowerShell启用多跳远程控制