node.js的世界,从callback开始,不会止于async.

所有人都在骂为什么不能完全进化,其实我感觉这就是老外的细心,为了承上。这也就是为什么async其实就是promise一样,假如不是一样的东西,如何承上启下。node.js异常处理一直为人诟病,其实为什么不能优雅心里没有*数吗?这就是设计得辣鸡....好吧,我只是一个用辣鸡的辣鸡。

有些东西不能改变,只能自己改变自己。google了一番,我大概找出了2种我自己喜欢的方式。

一、我只是console一下,我不处理。


async function getData(){
const a = await someFunction().catch((error)=>console.log(error));
const b = await someOtherFunction().catch((error)=>console.log(error));
if(a && b ) console.log("some result")
}

二、约定法则


const go = async () => {
const readFileResult = await sureThing(readFile('some.json'));
if (readFileResult.ok) {
const {
ok,
error,
data
} = await sureThing(parseJSON(data));
if (ok) {
// use our data
} else {
return {
ok,
error
};
}
} else {
return readFileResult;
}
};

以上2种方法,我觉得是比较适合现在的。第一种的不处理,显得更有佛性。而第二种,在保留了同步的写法,也可以处理异步,相得益彰。

打个广告: nodejs 学习群 atob('ODMwNTMxODc=')

https://medium.com/tech-buddy/async-await-without-try-catch-in-javascript-fdd38abf7e90

https://dzone.com/articles/easier-error-handling-using-asyncawait

最新文章

  1. [JavaCore] 微信手机浏览器版本判断
  2. 转帖:解决jquery.js在myeclipse中报错的问题
  3. opengpg
  4. js 字符串扩展
  5. asp.net 框架初接触
  6. php根据经纬度计算距离和方向--摘录自http://haotushu.sinaapp.com/post-520.html
  7. MongoDB用户
  8. 关于数据结构的10个面试题(c语言实现)
  9. hdu5698瞬间移动-(杨辉三角+组合数+乘法逆元)
  10. 1、Ansible简介及简单安装、使用
  11. stl集合算法
  12. HTML页面3秒后自动跳转的三种常见方法
  13. idea调试springmvc出现java.lang.ClassNotFoundException: org.springframework.web.context.ContextLoaderListener
  14. jQuery星级评论表单美化代码
  15. 【bzoj4676】 两双手
  16. htmlentities、addslashes 、htmlspecialchars的使用
  17. WebRTC网关服务器单端口方案实现
  18. CodeForces - 950C Zebras 模拟变脑洞的天秀代码
  19. Lua脚本语言入门学习其应用教程
  20. redis sentinel 高可用(HA)方案部署,及python应用示例

热门文章

  1. BZOJ 3626 LCA(离线+树链剖分)
  2. 分治FFT
  3. Visual Format Language(VFL)视图约束
  4. 【CF601C】Kleofáš and the n-thlon
  5. BZOJ #3746: [POI2015]Czarnoksiężnicy okrągłego stołu 动态规划
  6. Project Euler 453 Lattice Quadrilaterals 困难的计数问题
  7. Linux之系统信息操作20170330
  8. lua和C++的交互(1)
  9. springMVC的controller返回值
  10. epoll的一些细节和注意事项