- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我是 FP-TS 的新手,但仍然不太明白如何使用 TaskEither
.我正在尝试异步读取文件,然后使用 yaml-parse-promise 解析结果字符串。
==编辑==
我用文件的完整内容更新了代码以提供更多上下文并应用了 MnZrK 提供的一些建议。抱歉,我还是 FP-TS 的新手,我仍在努力使类型匹配。
现在我的错误是 map(printConfig)
线:
Argument of type '<E>(fa: TaskEither<E, AppConfig>) => TaskEither<E, AppConfig>' is not assignable to parameter of type '(a: TaskEither<unknown, AppConfig>) => Either<unknown, Task<any>>'.
Type 'TaskEither<unknown, AppConfig>' is not assignable to type 'Either<unknown, Task<any>>'.
Type 'TaskEither<unknown, AppConfig>' is missing the following properties from type 'Right<Task<any>>': _tag, rightts(2345)
import { pipe } from 'fp-ts/lib/pipeable'
import { TaskEither, tryCatch, chain, map, getOrElse } from "fp-ts/lib/TaskEither";
import * as T from 'fp-ts/lib/Task';
import { promises as fsPromises } from 'fs';
const yamlPromise = require('js-yaml-promise');
// const path = require('path');
export interface AppConfig {
service: {
interface: string
port: number
};
}
function readFileAsyncAsTaskEither(path: string): TaskEither<unknown, string> {
return tryCatch(() => fsPromises.readFile(path, 'utf8'), e => e)
}
function readYamlAsTaskEither(content: string): TaskEither<unknown, AppConfig> {
return tryCatch(() => yamlPromise.safeLoad(content), e => e)
}
// function getConf(filePath:string){
// return pipe(
// readFileAsyncAsTaskEither(filePath)()).then(
// file=>pipe(file,foldE(
// e=>left(e),
// r=>right(readYamlAsTaskEither(r)().then(yaml=>
// pipe(yaml,foldE(
// e=>left(e),
// c=>right(c)
// ))
// ).catch(e=>left(e)))
// ))
// ).catch(e=>left(e))
// }
function getConf(filePath: string): TaskEither<unknown, AppConfig> {
return pipe(
readFileAsyncAsTaskEither(filePath),
chain(readYamlAsTaskEither)
)
}
function printConfig(config: AppConfig): AppConfig {
console.log("AppConfig is: ", config);
return config;
}
async function main(filePath: string): Promise<void> {
const program: T.Task<void> = pipe(
getConf(filePath),
map(printConfig),
getOrElse(e => {
return T.of(undefined);
})
);
await program();
}
main('./app-config.yaml')
{ _tag: 'Right', right: Promise { <pending> } }
{ service: { interface: '127.0.0.1', port: 9090 } }
最佳答案
所有这些 e=>left(e)
和 .catch(e=>left(e))
是不必要的。
你的第二种方法更惯用。
// convert nodejs-callback-style function to function returning TaskEither
const readFile = taskify(fs.readFile);
// I don't think there is `taskify` alternative for Promise-returning functions but you can write it yourself quite easily
const readYamlAsTaskEither = r => tryCatch(() => readYaml(r), e => e);
function getConf(filePath: string): TaskEither<unknown, AppConfig> {
return pipe(
readFile(path.resolve(filePath)),
chain(readYamlAsTaskEither)
);
}
getConf
返回
TaskEither<unknown, AppConfig>
这实际上是一个
() => Promise<Either<unknown, AppConfig>>
.如果您有比
unknown
更具体的错误类型,然后改用它。
map
组合其他需要对配置进行的操作。或
chain
(即打印到控制台),然后应用一些错误处理来摆脱
Either
部分并最终获得
Task
(实际上只是懒惰
() => Promise
):
import * as T from 'fp-ts/lib/Task';
function printConfig(config: AppConfig): AppConfig {
console.log("AppConfig is", config);
return config;
}
function doSomethingElseWithYourConfig(config: AppConfig): TaskEither<unknown, void> {
// ...
}
async function main(filePath: string): Promise<void> {
const program: T.Task<void> = pipe(
getConf(filePath),
map(printConfig),
chain(doSomethingElseWithYourConfig),
// getting rid of `Either` by using `getOrElse` or `fold`
getOrElse(e => {
// error handling (putting it to the console, sending to sentry.io, whatever is needed for you app)
// ...
return T.of(undefined);
})
);
await program();
}
关于fp-ts - (已解决) 如何在 FP-TS 中链接依赖的 TaskEither 操作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57316857/
如何将多个 TS 文件合并为一个具有正确时间戳的 TS 文件?我可以将文件合并为一个,而无需接触数据。这将生成一个可播放的文件,但时间戳不正确,因此不会显示时间线(例如在 VLC 中)。因此,我需要重
有一个视频,其实是一个HLS流(TS文件的序列) 我想取出一个 TS block 并用另一个替换它。 另一个 block 将使用相同的 FFMPEG 编码设置进行编码。 如果你想知道我为什么需要这个:
这段代码取自https://en.cppreference.com/w/cpp/utility/variant/visit using var_t = std::variant; template s
我刚刚开始使用 ts-node。这是一个非常方便的工具。运行时间看起来很清晰。但它不适用于 CLI 解决方案。我无法将参数传递到编译的脚本中。 ts-node --preserve-symlinks
如果您愿意花 2 美分来解决调用另一个文件中定义的函数的问题,我将不胜感激。我阅读了 Typescript 的文档,其中建议设置一个接口(interface)或一个模块,然后引用它,但我收到了与下面相
我在努力 tsc ts/game.ts --out test.js --module amd 在我在 webstorm 中的 typescript 项目上,但是控制台没有显示任何错误,也没有输出 t
我已经意识到有一段时间了,一些 typescript 文件有一个 .d.ts 而其他的只有一个 .ts 扩展名。 它们代表什么?有什么区别? 最佳答案 这些是 declaration files ,或
是否有一种简单的方法可以将std::variant中包含的数据移动到std::variant中? 我想可以通过一个额外的类模板来切换Ts...中的所有类型,但是我想知道是否存在一种更优雅的就地方法。
我是 Angular 的新手,正在查看文档,但我想我也应该在这里问我的问题。 我正在导出一个越来越大的类,因此我想将其分成两个文件,并将 d1、d2、d3 等存储在一个单独的文件中,然后将它们导入到
无法通过指定 ts.t 找到条目(ts 是时间戳类型) 挖oplog,想搞清楚一秒钟有多少操作。 无法通过指定时间戳字段找到条目,其他字段可以。$在蒙戈外壳中: > db.oplog.rs.findO
这是我的 Angular5 项目结构。 tsconfig.app.json 和package.json 都包含这个部分 "include": [ "/src/main.ts",
我在 Angular 10 中有一个项目,遇到了奇怪的 TS (TSLint) 错误。在我使用 $localize 的任何地方的 Visual Studio Code 中,我都收到错误消息 Canno
所以我创建了一个快速服务器,它获取一个 mp3 文件(现在存储在本地,但稍后将从 mongo db 中获取)并使用 ffmpeg 制作 .m3u8 和 .ts 文件。文件成功发送到客户端,在客户端播放
我有一个 MY_FILE.ts像这样的文件: const someFunction = (param: MY_NAMESPACE.PARAM) : boolean => { // DO SOMETHI
我过去常常在运行前编译用 TS 编写的 e2e Protractor 测试,但现在我想出了如何在运行时编译 ts 文件——ts-node 似乎是一个很好的工具。正如许多文章所说,我在 Protract
我不熟悉 mod-rewrite,我找不到任何类似的问题或解决方案。 非常感谢... 模板: {query1}_{query2}.ts ts.php?v={query1}&seg={query2} 例
我有 firestore.service.ts,在一种方法中,我在 firebase 中创建和更新了一个集合……离开该方法后,我更改了集合的“id”,但我需要这个“id”是没有丢失,因为我需要它到下一
DefinitelyTyped 有许多库的类型定义,但当 Javascript 实现与 Typescript 分离时,我常常找不到使用它们的好方法,比如当库通过 a 将自己分配给窗口的属性时 标记,
我正在寻找一种在我发布到 NPM 时忽略项目中所有 .ts 文件的好方法,我可以通过将以下内容添加到我的 .npmignore 文件中来做到这一点: *.ts 但是等等......实际上我想在我的项目
我的 Yarn 工作区结构如下: /project package.json /packages /app package.json webpack.confi
我是一名优秀的程序员,十分优秀!