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);

Github

[Ramda] Handle Errors in Ramda Pipelines with tryCatch的更多相关文章

  1. [ES7] Handle Errors in Asynchronous Functions

    This lesson shows how regular control flow statements such as try/catch blocks can be used to proper ...

  2. [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 ...

  3. IOWebSocketChannel.connect handle errors

    https://github.com/dart-lang/web_socket_channel/issues/38 yes, my workaround is to create a WebSocke ...

  4. [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 ...

  5. [Ramda] Basic Curry with Ramda

    var _ = R; /***************************************** C U R R Y I N G E X A M P L E **************** ...

  6. 理解ASP.NET Core - 错误处理(Handle Errors)

    注:本文隶属于<理解ASP.NET Core>系列文章,请查看置顶博客或[点击此处查看全文目录](https://www.cnblogs.com/xiaoxiaotank/p/151852 ...

  7. 从函数式编程到Ramda函数库(二)

    Ramda 基本的数据结构都是原生 JavaScript 对象,我们常用的集合是 JavaScript 的数组.Ramda 还保留了许多其他原生 JavaScript 特性,例如,函数是具有属性的对象 ...

  8. [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 ...

  9. 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 ...

随机推荐

  1. Android基础&进阶

    基础总结篇之一:Activity生命周期 基础总结篇之二:Activity的四种launchMode 基础总结篇之三:Activity的task相关 基础总结篇之四:Service完全解析 基础总结篇 ...

  2. HTTP之request请求(注册)

    HTTP之request请求 request:请求 作用:获取浏览器发送过来的数据 组成部分:请求行 请求头 请求体 操作请求行 格式: 请求方式 请求资源 协议/版本 常用方法:HttpServle ...

  3. Codeforces 442A

    题目链接 A. Borya and Hanabi time limit per test 2 seconds memory limit per test 256 megabytes input sta ...

  4. sublime配置node开发环境

    下载node插件 https://github.com/tanepiper/SublimeText-Nodejs 下载zip压缩包后解压,文件名改为Node 打开文件“Nodejs.sublime-s ...

  5. Tumblr,instapaper分享

    <div id="bshare-custom"> <a title="分享到Tumblr" id="bshare-tumblr&qu ...

  6. AtCoder Beginner Contest 079 D - Wall【Warshall Floyd algorithm】

    AtCoder Beginner Contest 079 D - Wall Warshall Floyd 最短路....先枚举 k #include<iostream> #include& ...

  7. 记忆化搜索(DFS)--How many ways

    How many ways 这是一个简单的生存游戏,你控制一个机器人从一个棋盘的起始点(1,1)走到棋盘的终点(n,m).游戏的规则描述如下:1.机器人一开始在棋盘的起始点并有起始点所标有的能量.2. ...

  8. ADT上跑java application

    Invalid layout of java.lang.String at value## A fatal error has been detected by the Java Runtime En ...

  9. 3、.net core 部署到IIS

    1)下载对应的Hosting Bundle https://dotnet.microsoft.com/download/dotnet-core/2.2 2)VS发布项目,选择window平台环境 3 ...

  10. poj 3169 Layout (差分约束)

    3169 -- Layout 继续差分约束. 这题要判起点终点是否连通,并且要判负环,所以要用到spfa. 对于ML的边,要求两者之间距离要小于给定值,于是构建(a)->(b)=c的边.同理,对 ...