How do I avoid nested closures or clauses?

I need to take some suggestions from other programmers about something.

When we have to use an asynchronous closure, often because the Framework provides only an asynchronous method for something we can't do without, it seems to prevent us from using the guard statement, which we need to make the code easier to read. The guard statement keeps us from making nested if statements, which makes the code difficult to read if there are many lines of code within a set curly braces. I would see a closing curly brace, and not be able to see the what the if condition is for that if clause (what's between the set of curly braces). Putting comments at the closing curly brace works, but that means I would still have to find the if condition at the beginning of the if clause. I could collapse the clause where the curly braces closes a clause, but then I have to find that collapsed clause, because Xcode scrolls the content of the editor upward, so that the beginning of the clause is hidden upwards toward the top, and I don't know how far up it is. This problem that the guard statement solves is also a problem with completion handlers, which we have to have with any asynchronous code.

One approach to this problem is to avoid nested curly braces. How do I do that when I have to have asynchronous clause of code? If there is someway I can return a variable of type Result from a function the classical way instead of in a completion handler, I could avoid nested closures or clauses.

One way I've found is to use the DispatchSemaphore or any statement to instructs code execution to wait for an asynchronous closure end and then return. There's something I don't like about this that I'm not sure what it is. Anytime there is a code instruction to wait, it makes me uncertain to its effectiveness.

One thing that would help is if Xcode would show in the editor the beginning of collapsed code, so that when I click on the left side of the code editor to collapse a closure/clause when I'm seeing the bottom of the closure, Xcode would scroll downward so that the code editor shows me the code at the beginning of the clause, so that I wouldn't have to scroll upward to find the code that introduces the clause of code. I could file a bug report with Apple.

Anyone have any contributions to any solutions to this problem?

By the way, what is the Apple or Swift word for I'm calling a clause? What, for example, is the code that is enclosed between curly braces? Am I using the words clause and closure each time I used them?

What, for example, is the code that is enclosed between curly braces? Am I using the words clause and closure each time I used them?

If I understand your question, code between curly is closure. A single clause is an instruction.

Have you looked at async/await pattern to answer your question ?

No. Do you have a source of information about that that you particularly like more than other sources?

I appreciate RayWanderlich tutorials, like this one:

https://www.raywenderlich.com/books/modern-concurrency-in-swift/v1.0/chapters/2-getting-started-with-async-await

How do I avoid nested closures or clauses?
 
 
Q