Using async effects in TypeScript

[SOLVED]

Hello ๐Ÿ‘‹

Give me a little hand here.

const UserService = Layer.sync(UserServiceTag, () => ({
  users: [], 
  findOne(filter: Partial<User>) {
    return Effect.promise((function(this: UserService) {
      const {promise, resolve} = Promise.withResolvers<User | undefined>()
      const result = this.users.find((user: User) => {
        return Object.keys(filter).every(key => user[key as keyof User] === filter[key as keyof User])
      })
      resolve(result)
      return promise
    }).bind(this))
  },
  findOneValidate(filter: Partial<User>) {
    return pipe(
       this.findOne(filter),
       Effect.promise(function(this: UserService, user: User | undefined) {
         return {
           ...user,
           async validatePassword: (password: string) => password === user?.password // implement bcrypt.compare
         }
       })
    )
  }
}))


It's supposed to be easy, but I've tried many combos and nothing.
I need findOneValidate to call findOne, consume its effect, and then use the user: User | undefined it returns,
and finally return an object with the user fields spread, and a validatePassword method.

The service is typed as:

type UserService = {
  users: User[]
  findOne: (filter: any) => Effect.Effect<User | undefined, never, never>
  findOneValidate: (filter: any) => Effect.Effect<(User & {validatePassword:(password: string) => Promise<boolean>}) | undefined, never, never>
}


With this I intend to learn how to call one effect from another in an async environment. Thanks! effect

[SOLVED]
Was this page helpful?