Stupid question about delay functions

I’m trying to implement the delay functions that you can find recommended by various sources.  What am I doing wrong?

 

In a playground or my test app, the Before and After lines print but the Inside never does.

 

The simplest form is this:

print("Before the delay")   // Prints
DispatchQueue.main.asyncAfter(deadline: .now() + 8.0) {
    print("Inside the delay")   // Never prints
}
print("After the delay")   // Prints

 

 

I’ve also tried these configurations in both playground and test app:

func delay(_ delay: Double, closure: ()->()) {
    print("A delay of (delay) seconds has been called for")  // This prints
    DispatchQueue.main.asyncAfter(deadline: .now() + delay) {
        print("Inside the delay")  // This does not print
    }
}

func delayX(time: Double, closure:@escaping ()->()) {
    print("A delayX of (time) seconds has been called for"))  // This prints
    DispatchQueue.main.asyncAfter(deadline: .now() + time) {
         print("Inside the delay")  // This does not print
     closure()
    }
}

delay(3.0) {
    print("Inside the delay") // This does not print
}
delayX(5.0) {
    print("Inside the delayX")  // This does not print
}

Powered by WPeMatico

You may also like...

Comments are closed.