Troubleshooting Fibre Error in Effect Framework Code
I am getting Fibre error on this code. Any idea ?
import { Effect, Console, Either, pipe, Exit, Context } from 'effect';
import * as S from '@effect/schema/Schema';
const USER_POSTS_URL = 'https://jsonplaceholder.typicode.com/posts';
class FetchError {
readonly _tag = 'FetchError'
};
class JSONError {
readonly _tag = 'JSONError'
}
const userPostSchema = S.struct ({
userId: S.number,
id: S.number,
title: S.string,
body: S.string,
});
const userPostsSchema = S.array (userPostSchema);
type UserPost = S.Schema.Type<typeof userPostSchema>;
type UserPosts = S.Schema.Type<typeof userPostsSchema>;
const parseUserPosts = S.decodeUnknown (userPostsSchema);
interface UserService {
readonly getUserPosts: () => Effect.Effect<UserPosts>,
}
const UserServiceTag = Context.Tag<UserService>();
function fetchUserPosts () {
return Effect.tryPromise ({
try: (signal) => fetch (USER_POSTS_URL, { signal }),
catch: () => new FetchError (),
});
}
function userPostsJson (response: Response) {
return Effect.tryPromise ({
try: () => response.json (),
catch: () => new JSONError (),
});
}
function myCode () {
const program = Effect.gen (function* ($) {
const userService = yield* $(UserServiceTag);
const userPosts = yield* $(userService.getUserPosts ());
const reqPosts = userPosts.filter (({ userId }) => userId === 10);
return reqPosts;
});
const prg = program.pipe (Effect.provideService (UserServiceTag, {
getUserPosts: () => Effect.gen (function* ($) {
const response = yield* $(fetchUserPosts ());
const uPostsJson = yield* $(userPostsJson (response));
const userPosts = yield* $(parseUserPosts (uPostsJson));
return userPosts;
}),
}));
return Effect.runPromise (prg);
}
export default function check9 () {
const result = myCode ();
return result.then (x => console.log ('done', x));
}import { Effect, Console, Either, pipe, Exit, Context } from 'effect';
import * as S from '@effect/schema/Schema';
const USER_POSTS_URL = 'https://jsonplaceholder.typicode.com/posts';
class FetchError {
readonly _tag = 'FetchError'
};
class JSONError {
readonly _tag = 'JSONError'
}
const userPostSchema = S.struct ({
userId: S.number,
id: S.number,
title: S.string,
body: S.string,
});
const userPostsSchema = S.array (userPostSchema);
type UserPost = S.Schema.Type<typeof userPostSchema>;
type UserPosts = S.Schema.Type<typeof userPostsSchema>;
const parseUserPosts = S.decodeUnknown (userPostsSchema);
interface UserService {
readonly getUserPosts: () => Effect.Effect<UserPosts>,
}
const UserServiceTag = Context.Tag<UserService>();
function fetchUserPosts () {
return Effect.tryPromise ({
try: (signal) => fetch (USER_POSTS_URL, { signal }),
catch: () => new FetchError (),
});
}
function userPostsJson (response: Response) {
return Effect.tryPromise ({
try: () => response.json (),
catch: () => new JSONError (),
});
}
function myCode () {
const program = Effect.gen (function* ($) {
const userService = yield* $(UserServiceTag);
const userPosts = yield* $(userService.getUserPosts ());
const reqPosts = userPosts.filter (({ userId }) => userId === 10);
return reqPosts;
});
const prg = program.pipe (Effect.provideService (UserServiceTag, {
getUserPosts: () => Effect.gen (function* ($) {
const response = yield* $(fetchUserPosts ());
const uPostsJson = yield* $(userPostsJson (response));
const userPosts = yield* $(parseUserPosts (uPostsJson));
return userPosts;
}),
}));
return Effect.runPromise (prg);
}
export default function check9 () {
const result = myCode ();
return result.then (x => console.log ('done', x));
}message.txt2.04KB
