[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 ...
随机推荐
- SDUT-3379_数据结构实验之查找七:线性之哈希表
数据结构实验之查找七:线性之哈希表 Time Limit: 1000 ms Memory Limit: 65536 KiB Problem Description 根据给定的一系列整数关键字和素数p, ...
- pytest fixture 利用 params参数实现用例集合
@pytest.fixture有一个params参数,接受一个列表,列表中每个数据都可以作为用例的输入.也就说有多少数据,就会形成多少用例.如下面例子,就形成3条用例 test_parametrizi ...
- height自适应
如果子元素没有设置 float 属性啥的,父元素就是自动适应子元素宽高的. 子元素如果全是浮动属性(float),那么父元素就没有高度. 除非,你在子元素最后加一个清除浮动( <div styl ...
- oracle如何检查用户是否用了默认密码
如果使用默认密码,很可能就对你的数据库造成一定的安全隐患,那么可以使用如下的查询获得那些用户使用默认密码 select username "User(s) with Default Pass ...
- ffmpeg在iOS的使用 - iFrameExtractor源码解析
http://www.cocoachina.com/ios/20150914/13284.html iFrameExtractor地址:https://github.com/lajos/iFrameE ...
- @hdu - 6687@ Rikka with Stable Marriage
目录 @description@ @solution@ @accepted code@ @details@ @description@ 给定一个稳定婚姻匹配问题,其中第 i 个男生与第 j 个女生之间 ...
- LeetCode93 Restore IP Addresses
题目: Given a string containing only digits, restore it by returning all possible valid IP address com ...
- 基本数据类型的值传递 和引用数据类型的引用传递 Day06
ValueTest1.java package com.sxt.valuetest; /* * 基本数据类型的传递:传递的是值得副本 */ public class ValueTest1 { publ ...
- Python 变量赋值
- oracle函数 CONVERT(c1,set1,set2)
[功能]将源字符串c1 从一个语言字符集set2转换到另一个目的set1字符集 [参数]c1,字符串,set1,set2为字符型参数 [返回]字符串 [示例] select convert('stru ...