[Node] Catch error for async await
When we try to do MongoDB opration, mongoose return Promise, we can use async/await to simply the code:
const mongoose = require('mongoose');
const Store = mongoose.model('Store');
exports.createStore = async (req, res) => {
const store = new Store(req.body);
await store.save();
res.redirect('/');
};
The problme here is no error handling, we can use try catch, there is another approach is function composion.
We can define a high order function:
exports.catchErrors = (fn) => {
return function(req, res, next) {
return fn(req, res, next).catch(next);
};
};
We just need to wrap express router:
router.post('/add', catchErrors(storeCtrl.createStore));
So the following middlewares can handle those error later.
[Node] Catch error for async await的更多相关文章
- Node 7.6默认支持Async/Await
Node.js 7.6正式默认支持async/await功能,并能够使低内存设备获得更出色的性能. Node 7.6对async/await的支持来自于将V8(Chromium JavaScript引 ...
- (译文)学习ES6非常棒的特性——Async / Await函数
try/catch 在使用Async/Await前,我们可能这样写: const main = (paramsA, paramsB, paramsC, done) => { funcA(para ...
- 优雅地 `async/await`
async/await 虽然取代了回调,使用类似同步的代码组织方式让代码更加简洁美观,但错误处理时需要加 try/catch. 比如下面这样,一个简单的 Node.js 中使用 async/await ...
- 【转】6 Reasons Why JavaScript’s Async/Await Blows Promises Away (Tutorial)
原文:https://hackernoon.com/6-reasons-why-javascripts-async-await-blows-promises-away-tutorial-c7ec105 ...
- promise 进阶 —— async / await 结合 bluebird
一.背景 1.Node.js 异步控制 在之前写的 callback vs async.js vs promise vs async / await 里,我介绍了 ES6 的 promise 和 ES ...
- Fetch API & Async Await
Fetch API & Async Await const fetchJSON = (url = ``) => { return fetch(url, { method: "G ...
- ES next & Async Await
ES next & Async Await https://jestjs.io/docs/en/asynchronous#async-await ES7 new async () => ...
- webpack & async await
webpack & async await ES 7 // async function f() { // return 1; // } const f = async () => { ...
- 图与例解读Async/Await
JavaScript ES7的async/await语法让异步promise操作起来更方便.如果你需要从多个数据库或者接口按顺序异步获取数据,你可能最终写出一坨纠缠不清的promise与回调.然而使用 ...
随机推荐
- Shiro结合Redis解决集群中session同步问题
pom.xml文件中引入redis的依赖 在application.xml配置redis: <bean id="jedisConnectionFactory" class=& ...
- Istio Service Mash管理微服务
Istio Service Mash管理微服务 今天的文章通过Istio开源项目展示如何为Kubernetes管理的微服务提供可见性,弹性,安全性和控制. 服务是现代软件体系结构的核心.比起复杂庞大的 ...
- reboot---重启Linux系统
reboot命令用来重新启动正在运行的Linux操作系统. 语法 reboot(选项) 选项 -d:重新开机时不把数据写入记录文件/var/tmp/wtmp.本参数具有“-n”参数效果: -f:强制重 ...
- django第三方库
1. django_celery_beat 作用:网页端配置定时任务 注意:1,需要迁移表格 2.需要注册app python3 manage.py makemigrations python3 ma ...
- 【CS Round #39 (Div. 2 only) A】Removed Pages
[Link]: [Description] [Solution] 每读入一个x; 把a[(x-1)/2]置为1即可; 统计1的个数 [NumberOf WA] [Reviw] [Code] /* */ ...
- [置顶]
Docker学习总结(1)——Docker实战之入门以及Dockerfile(一)
一.Docker是什么? 首先Docker是软件工业上的集装箱技术 回顾,在没有集装箱出现以前,传统运输行业中,会存在这些问题: 在运输过程中,货物损坏 装卸.运输货物,效率低下 运输手续繁多及运输环 ...
- UVA 10306 e-Coins(全然背包: 二维限制条件)
UVA 10306 e-Coins(全然背包: 二维限制条件) option=com_onlinejudge&Itemid=8&page=show_problem&proble ...
- jQuery返回值:jQuery对象
$(function(){ //返回值 alert($); //jQuery //以下返回的全是jQuery对象 alert($()); alert($('#box')); alert($('#box ...
- node 内存溢出
遇到这个问题的人可以更快解决 再复制写一篇 利于百度搜索 坑爹的node 内存溢出 react开发项目 安装一个插件依赖 ,然后就报错了 报错如下(自己的没有截图出来 这是从别人的截图---报错基本 ...
- 解读OpenRTB(实时竞价)生态系统
最近3年,广告实时竞价(RealTimeBidding)模式逐渐流行起来. 2012年大致了解过,最近一段时间,重新温习下. 半壁江山 生态系统总的来说分为2个部分,卖方和买方. 卖方:媒体,即拥有广 ...