首页 / JAVASCRIPT / [JS Compose] 3. Use chain for composable error handling with nested Eithers (flatMap)
[JS Compose] 3. Use chain for composable error handling with nested Eithers (flatMap)
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了[JS Compose] 3. Use chain for composable error handling with nested Eithers (flatMap),小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含3592字,纯文字阅读大概需要6分钟。
内容图文
![[JS Compose] 3. Use chain for composable error handling with nested Eithers (flatMap)](/upload/InfoBanner/zyjiaocheng/1051/76a4bd54ff454f23ac01087585cfbc2f.jpg)
We refactor a function that uses try/catch to a single composed expression using Either. We then introduce the chain function to deal with nested Eithers resulting from two try/catch calls.
For example we have this code using try & catch:
const getPort = () => { try { const file = fs.readFileSync(‘config.json‘); const c = JSON.parse(file); return c.port; } catch(e) { return3000; } }
And now, we want to use Either to rewirte the code:
// SETUP: fake fs // ========== const fs = { readFileSync: name => { if(name === ‘config.json‘) { return JSON.stringify({port: 8888}) } else { throw(‘missing file!‘) } } } //=================const Right = x => ({ map: f => Right(f(x)), fold: (f, g) => g(x), toString: () => `Right(${x})` }); const Left = x => ({ map: f => Left(x), fold: (f, g) => f(x), toString: () => `Left(${x})` }); const fromNullable = x => x != null ? Right(x): Left(null); const tryCatch = f => { try { return Right(f()); } catch(e) { return Left(e); } } //=========================const getPort = () => tryCatch(() => fs.readFileSync(‘config.json‘)) .map(f => JSON.parse(f)) .fold( x => 3000, x => x.port); console.log(getPort(‘config.json‘))
We wrote the function ‘tryCatch‘, the idea is put the code need to be checked in try, when success call ‘Right‘, error, call ‘Left()‘.
tryCatch(() => fs.readFileSync(‘config.json‘))
Read the file, is success, will return file content. If not, then goes to set default 3000.
.fold( x => 3000, x => x.port);
It works, but we still miss one things, in the old code, the ‘JSON.parse‘ are also wrapped into try catch.
const getPort = () => tryCatch(() => fs.readFileSync(‘config.json‘)) //Right(‘{port:8888}‘) .map(f => tryCatach(() => JSON.parse(f))) //Right(Right({port:8888}))
But once we also wrap parseing code into tryCatch function, the return value is 2d-Right. So we want to flatten it.
const Right = x => ({ map: f => Right(f(x)), flatMap: f => f(x), fold: (f, g) => g(x), toString: () => `Right(${x})` }); const Left = x => ({ map: f => Left(x), flatMap: f => f(x), fold: (f, g) => f(x), toString: () => `Left(${x})` });
We add ‘flatMap‘, so instead of putting the value into Right() or Left(), we just return the value. Because we know the value passed in is already a Right or Left.
const getPort = () => tryCatch(() => fs.readFileSync(‘config.json‘)) //Right(‘{port:8888}‘) .flatMap(f => tryCatch(() => JSON.parse(f))) //Right({port:8888}) .fold( x => 3000, x => x.port);
---------
// SETUP: fake fs // ========== const fs = { readFileSync: name => { if(name === ‘config.json‘) { return JSON.stringify({port: 8888}) } else { throw(‘missing file!‘) } } } //=================const Right = x => ({ map: f => Right(f(x)), flatMap: f => f(x), fold: (f, g) => g(x), toString: () => `Right(${x})` }); const Left = x => ({ map: f => Left(x), flatMap: f => f(x), fold: (f, g) => f(x), toString: () => `Left(${x})` }); const fromNullable = x => x != null ? Right(x): Left(null); const tryCatch = f => { try { return Right(f()); } catch(e) { return Left(e); } } //=========================const getPort = () => tryCatch(() => fs.readFileSync(‘config.json‘)) //Right({port:8888}) .flatMap(f => tryCatch(() => JSON.parse(f))) //Right(Right({port:8888})) .fold( x => 3000, x => x.port); console.log(getPort(‘config.json‘))
原文:http://www.cnblogs.com/Answer1215/p/6175746.html
内容总结
以上是互联网集市为您收集整理的[JS Compose] 3. Use chain for composable error handling with nested Eithers (flatMap)全部内容,希望文章能够帮你解决[JS Compose] 3. Use chain for composable error handling with nested Eithers (flatMap)所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。