I have a function lets call it "a" that runs some code and then returns a string "x" is updated in an asynchronous code block and then returned.
How would I go about making the program wait to return x until after the asynchronous code runs?
func a() -> String {
//code
//code
var x: String
async block {
x = "test"
}
return x
}
You can use completion closure for this
func a(completion: @escaping (_ value:String)->()) {
var x: String = ""
async block {
x = "test"
completion(x) //when x has new value
}
}
//Call like this (value will be executed when the completion block is returned
a { (value) in
print(value)
}