Swift 3 - Wait for the server result

Viewed 1655

As I could wait for the server to return a result and only then execute another function (which will use data from the server response).

Something like:

Func A: I get response from the server: responseCode = 200, which stores in a variable "code" Func B: check if the variable "code" is 200.

I can not find the way and so far I always run func B before it runs.

Thank you very much.

 @IBAction private func bt_signin () {
    print("Step 1")
    func_a(text: url_text)
    func_b()
}


private func func_a(text: String) {

print("Step 2")

let url = NSURL(string: text)
let request = NSMutableURLRequest(url: url! as URL)

let task = URLSession.shared.dataTask(with: request as URLRequest){ data, response, error in
    guard error == nil && data != nil else
    {
        print("Error: ", error)
        return
    }

    let httpStatus = response as? HTTPURLResponse

    if httpStatus!.statusCode == 200
    {
        self.responseCode = httpStatus!.statusCode
        print("Step 3")                
    }
}
task.resume()
}

 func func_b (){
    print("Step 4")
    if (responseCode == 200) {
        print("Step 5")
    }
    }

I get the following:

Step 1
Step 2
Step 4
Step 3
3 Answers
Related