[Ramda] Handle Errors in Ramda Pipelines with tryCatch
Handling your logic with composable functions makes your code declarative, leading to code that's easy to read and easy to test. Breaking that up to wrap some risky function in a try/catch block introduces imperative code and makes it harder to maintain that declarative approach. With Ramda's tryCatch function, you can handle errors right in the middle of a composition and leave your code clean and functional. We'll also see how you can use propOr to avoid common "cannot find X of undefined" errors.
const R = require('ramda');
const person = {
id: ,
name: 'Joe'
};
/*
* The problem for this code:
* R.prop('name') assume that the object passed in has 'name' prop
* But what if the object is undefined? Then we will get error.
*
* Solution: R.tryCatch()
* */
/*
const getPersonName = R.prop('name');
const getUpperCaseName = R.pipe(
getPersonName,
R.toUpper
);
const res = getUpperCaseName(person); // ERROR
*/
const getPersonName = R.tryCatch(R.prop('name'), R.always('Default'));
const getUpperCaseName = R.pipe(
getPersonName,
R.toUpper
);
const res = getUpperCaseName(undefined); // DEFAULT
console.log(res);
[Ramda] Handle Errors in Ramda Pipelines with tryCatch的更多相关文章
- [ES7] Handle Errors in Asynchronous Functions
This lesson shows how regular control flow statements such as try/catch blocks can be used to proper ...
- [Ramda] Handle Branching Logic with Ramda's Conditional Functions
When you want to build your logic with small, composable functions you need a functional way to hand ...
- IOWebSocketChannel.connect handle errors
https://github.com/dart-lang/web_socket_channel/issues/38 yes, my workaround is to create a WebSocke ...
- [Vue @Component] Handle Errors and Loading with Vue Async Components
Because async components are not bundled with your app, they need to be loaded when requested. This ...
- [Ramda] Basic Curry with Ramda
var _ = R; /***************************************** C U R R Y I N G E X A M P L E **************** ...
- 理解ASP.NET Core - 错误处理(Handle Errors)
注:本文隶属于<理解ASP.NET Core>系列文章,请查看置顶博客或[点击此处查看全文目录](https://www.cnblogs.com/xiaoxiaotank/p/151852 ...
- 从函数式编程到Ramda函数库(二)
Ramda 基本的数据结构都是原生 JavaScript 对象,我们常用的集合是 JavaScript 的数组.Ramda 还保留了许多其他原生 JavaScript 特性,例如,函数是具有属性的对象 ...
- [React] Handle React Suspense Errors with an Error Boundary
Error Boundaries are the way you handle errors with React, and Suspense embraces this completely. Le ...
- Laravel API Errors and Exceptions: How to Return Responses
Laravel API Errors and Exceptions: How to Return Responses February 13, 2019 API-based projects are ...
随机推荐
- 【风马一族_php】数组函数
原文来自:http://www.cnblogs.com/sows/p/6045699.html (博客园的)风马一族 侵犯版本,后果自负 2016-11-09 15:56:26 数组 函数 php- ...
- Servlet容器container
通俗点说,所谓容器,就是放东西的地方.Servlet容器自然就是放Servlet的地方.J2EE开发,是有分工的.一般的程序员,写得都是应用开发,我们会按照一定的规则,开发我们的系统,比如用Servl ...
- left join table_a on ...like...
- oracle编写分页过程
有了上面的基础,相信大家可以完成分页存储过程了,要求,请大家编写一个存储过程,要求可以输入表名.每页显示记录数.当前页.排序字段(deptno降序).返回总记录数,总页数和返回结果集. 把一个字符串, ...
- @codeforces - 1276F@ Asterisk Substrings
目录 @description@ @solution@ @accepted code@ @details@ @description@ 给定一个包含 n 个小写字母的字符串 s,用 s 生成 n 个串 ...
- 跨域知识(一)——CORS
CORS是一个W3C标准,全称是"跨域资源共享"(Cross-origin resource sharing). 它允许浏览器向跨源服务器,发出XMLHttpRequest请求,从 ...
- 设置onselectstart在ie浏览器下对于input及textarea标签页会生效
设置onselectstart在ie浏览器下对于input及textarea标签页会生效, 可以使用js来控制对于某种标签不生效,代码如下: document.onselectstart = disa ...
- iOS:学习runtime的理解和心得
http://www.cocoachina.com/ios/20150901/13173.html 作者:兴宇是谁 授权本站转载. Runtime是想要做好iOS开发,或者说是真正的深刻的掌握OC这门 ...
- Docker容器中安装新的程序
在容器里面安装一个简单的程序(ping). 之前下载的是ubuntu的镜像,则可以使用ubuntu的apt-get命令来安装ping程序:apt-get install -y ping. $docke ...
- HZOJ Weed
作者的题解: 如果一段操作被执行,会对整个栈有什么影响呢? 把栈弹出若干个数后再插入若干个数. 线段树: 每个点纪录三个值:执行完这段操作后会删多少个,再插多少个,插的和一共是多少. 合并值时再用一个 ...