1

Does fp-ts have any functionality to do a while loop (stack safe) with timeout. Like tail recursion, Y Combinator or something. I have seen ChainRec, but there aren't really any documentation besides functions signatures.

Exact issue I have: I need to wait for the database (postgres) to start up in docker container. I decided to determine that by writing a loop that scans the output of docker logs for a specific regex each n milliseconds, and the loop stops if the regex is matched or timeout has been reached. Timeout, regex and n are all arguments.

Or maybe it's better to do docker logs --follow and process STDIN stream. But I don't know how to do it functionally, either.

P.S. Trying to execute something in database instead of scanning for logs doesn't work. Since postgres for some reason becomes available during its boot sequence and then becomes unavailable again before it finally completely starts, leaving a message in the log.

Danil
  • 103
  • 7
  • `fp-ts` (and functional programming in general) handles looping via recursion and higher order functions. It's a bit tricky to give an answer without knowing the specific issue you are facing, if you can update the question with some more concrete details I'd be happy to try to point you in the right direction. – bugs Feb 28 '22 at 14:13
  • @bugs I have updated the question with specific details. Thanks in advance – Danil Feb 28 '22 at 14:29

1 Answers1

0

One functional way would be to use setInterval with Promises:

await new Promise((resolve) => {
  const a = setInterval(() => {
    if(serviceIsAvailable()) {
      clearInterval(a)
      resolve(true)
    }
  }, 500)
})

If you want, you could add a reject case, a deadline (reject if not satisfied after XYZ seconds), etc.

user1713450
  • 1,307
  • 7
  • 18