Handling API Call Retries with Conditional Array Updates in TypeScript

Hey Effect community!

Does anyone know how I should approach a problem where
1) have an array of contents
2) make API call with the array
3) if API response isn't valid structure -> add an item to array of contents
4) retry the chain WITHOUT adding any more items to the array even if it fails

Currently I have a Ref, and then Effect.retry, but I think I'm in over my depth how to model such bit more complicated things

const messagesRef = makeMessages([])

  const px = messagesRef.pipe(
    Effect.andThen((messages) => messages.get.pipe(
      Effect.andThen((messages: string[]) => getApiResult(messages)),
      Effect.andThen((text: string) => extractMetadata(text)),
      Effect.orElse(() => messages.add)
    )),
  )

// Retry above 3 times
const retriedEffect = Effect.retry(px, { times: 3 })


The problem is that retriedEffect just becomes Successful and the value is
undefined
(return type of messages.add)... It should not be successful when the orElse is called..

Thank you in advance!!
Was this page helpful?