博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
[Javascript Crocks] Flatten Nested Maybes with `chain`
阅读量:6943 次
发布时间:2019-06-27

本文共 1650 字,大约阅读时间需要 5 分钟。

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: 200,            body: {                id: 1,                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(1)    .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 '' in Crocks.

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

 

转载地址:http://onanl.baihongyu.com/

你可能感兴趣的文章
SQL数据库语言总结及代码示例
查看>>
PHP开发APP接口
查看>>
功能测试报告
查看>>
获取手机联系人信息
查看>>
关于64位操作系统,应用程序占用内存飙升的问题解决方法记录
查看>>
Quartz.NET 调度配置说明
查看>>
统计Redis中各种数据大小
查看>>
JFinal-Beetl-Shiro(JdbcRealm)-例子
查看>>
java中的内部类总结
查看>>
跬步之积,以至千里
查看>>
Windows和Linux设计和原理哪个系统更先进呢?
查看>>
1234
查看>>
原 shell学习四运算符
查看>>
var与Javascript变量隐式声明
查看>>
InstallShield安装包中集成第三方安装包的方案选择
查看>>
windows下 sublime text2 跳转查看go源码
查看>>
c语言中的面向对象(1)----kobject 和 container_of
查看>>
单利模式
查看>>
kafka client使用kerberos
查看>>
修复ThinkPad一键恢复功能
查看>>