In a loop, call a function having a completion handler, order the result

I have the following problem, maybe there exists a solution. I need to call a given function with a completion handler in a loop from synchronous code. The problem is that the function calls might not complete in order, and I need to find a way to be able to know the order of the… Read More In a loop, call a function having a completion handler, order the result

Wait for completion handler to finish before returning

func checkUsername(username: String) -> Bool { var avalible = true Database.database().reference().child("usernames").child(username).observeSingleEvent(of: .value, with: { snapshot in if snapshot.exists() { print("exists") avalible = false } }) return available } I’m trying to check if a username exists. I’ve already tried multiple things, but it seems like the function always returns before the completion handler even finishes… Read More Wait for completion handler to finish before returning