[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与回调.然而使用 ...
随机推荐
- three.js 运行3D模型
HTML <!DOCTYPE html> <html style="height: 100%;"> <head> <title>m ...
- Ajax缓存原理
一.什么是Ajax缓存原理? Ajax在发送的数据成功后,会把请求的URL和返回的响应结果保存在缓存内,当下一次调用Ajax发送相同的请求时,它会直接从缓存中把数据取出来,这是为了提高页面的响应速度和 ...
- ie中 专有的注释
http://www.cnblogs.com/liluping860122/p/3539165.html
- POJ——T 2752 Seek the Name, Seek the Fame
http://poj.org/problem?id=2752 Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 20418 ...
- Struts2+Spring+Hibernate step by step 06 整合Hibernate
注:该系列教程.部分内容来自王健老师编写ssh整合开发教程 Hibernate是一款优秀的ORM(Object Relation Mapping-对象关系映射图)工具.与Struts.Spring项目 ...
- JS-网页中分页栏
原理 三部分 我给分页栏分成了3部分 上一页:调用prePage()函数 下一页:调用nextPage()函数 带有数字标识的部,调用skipPage()函数 prePage函数 function p ...
- Android开发之经常使用开源库直接拿来用
1.from 代码家 整理比較好的源代码连接 **************************************************************************** ...
- oracle跨数据库跨用户訪问注意事项
java代码中不同意出现oracle的username.数据链路名. 跨用户.跨数据库的訪问必须在oracle中建同义词或视图来实现.在java代码中仅仅需当做当前用户下的对象处理.
- Filebeat之input和output(包含Elasticsearch Output 、Logstash Output、 Redis Output、 File Output和 Console Output)
前提博客 https://i.cnblogs.com/posts?categoryid=972313 Filebeat啊,根据input来监控数据,根据output来使用数据!!! Filebeat的 ...
- 错误 make: Nothing to be done for 'default'
Makefile书写格式非常严格,all:<TAB缩进>make -C $(KDIR) M=$(PWD) $(EXTRA_CFLAGS) modulesdefault:<TAB缩进& ...