Sometimes, we run into situations where we end up with a Maybe within the context of another Maybe. Nested structures like this can get really confusing to work with. In this lesson, we’ll look at an example of this and see how chaincan help us out by flattening the nested Maybe

As we all know, inside an array return another array is nested array.

Inside observable return another observable get Observable of Observable.

The same inside Just return another Just, we get Just of Just:

const prop = require('crocks/Maybe/prop');
const propPath = require('crocks/Maybe/propPath'); /**
* return Maybe type
*/
const getUser = id =>
new Promise((resolve, reject) => {
const result = {
status: ,
body: {
id: ,
username: 'tester',
email: 'test@gmail.com',
address: {
street: '111 E. West St',
city: 'Anywhere',
state: 'PA',
postalCode: '19123-4567'
}
}
}
resolve(prop('body', result)); // return Just {}
}); const getPostalCode = propPath(['address', 'postalCode']); getUser()
.then(user => user.map(getPostalCode)) // map to Just {} --> Just Just '19123-4567'
.then(console.log); // Just Just "19123-4567"

Inside getUser function we return a Just type object. Then from propPath, we get Just Just type object. That's why we need flatten it.

The way to solve the problem is using flat map which is called 'chain' in Crocks.

getUser()
.then(user => user.chain(getPostalCode).option("not available"))
.then(console.log); // "19123-4567"

最新文章

  1. 基于vw的响应式排版布局
  2. jdbc基本查询方法
  3. zepto.js之ajax剖析
  4. BZOJ3226[Sdoi2008]校门外的区间 题解
  5. tomcat 调优
  6. insert时出现主键冲突的处理方法【转载】
  7. HTML入门知识汇总
  8. 【Thymeleaf】Thymeleaf模板对html实时刷新
  9. python中深拷贝与浅拷贝
  10. Sublime Text3—常用插件Emmet
  11. session和cookies
  12. Golang 实现 set 集合,变相实现 切片去重、排序 功能
  13. python 类似java的三目运算符
  14. 阿里云服务器上安装mysql的详细步骤
  15. hdoj1005(循环,找规律)
  16. 【工具推荐】截图工具 Snipaste
  17. 2018.06.29 NOIP模拟 排列(线段树)
  18. ZigBee 学习资源
  19. streamsets stream selector 使用
  20. nginx实现openfire负载均衡

热门文章

  1. python 内存泄露的诊断
  2. 在linux查看内存的大小
  3. go之switch
  4. 强迫症!一行代码拿到url特定query的值
  5. 【CodeForces688A】Opponents
  6. Python简介和基础入门
  7. Selenium获取input值的两种方法:WebElement.getAttribute("value")和WebElement.getText()
  8. .Net Core 学习(二)上篇
  9. https 结合使用 对称加密和非对称加密
  10. P3709 大爷的字符串题(50分)