Pausing a function until a condition is met

Hi all I have a function A which checks the state of a const (useState) before proceeding.

the state controls whether a modal is open or closed. and if the modal is closed then the function can proceed. I have googled plenty ways but none seem to work. useeffect canโ€™t work here because the function in question has already started then pauses to check if a condition is met (the modal is closed) before continuing.

I assume the issue is that the state of whether the modal is open or closed takes a while to update. How do i resolve this

I would recommend adding some console logs to see how the state values are getting changed.