我想以某种方式包装fp中的Fetch API:
import * as TE from 'fp-ts/lib/TaskEither';
import * as E from 'fp-ts/lib/Either';
import { flow } from 'fp-ts/lib/function';
import { pipe } from 'fp-ts/lib/pipeable';
const safeGet = (url: string): TE.TaskEither<Error, Response> => TE.tryCatch(
() => fetch(url),
(reason) => new Error(String(reason))
);
const processResponce = flow(
(x: Response): E.Either<Error, Response> => {
return x.status === 200
? E.right(x)
: E.left(Error(x.statusText))
},
TE.fromEither
);
export const httpGet = (url: string): TE.TaskEither<Error, Response> => pipe(
safeGet(url),
TE.chain(processResponce)
);在这个例子中,在运行httpGet之后,我会得到一个响应,并且需要手动地使用.json()方法。那么,我如何避免这种行为,让json进入管道呢?
发布于 2020-07-14 21:18:47
把这个锁在你的流量里?
const safeJson = <T = unknown>(resp: Response): TE.TaskEither<Error, T> => TE.tryCatch(
() => resp.json(),
(reason) => new Error(String(reason))
);https://stackoverflow.com/questions/62155842
复制相似问题