Last active
October 10, 2016 08:19
-
-
Save skreutzberger/ab8f6aa7d4b222e99f24f28e47c0a68b to your computer and use it in GitHub Desktop.
A Vapor route with async code
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// home route | |
app.get("/") { request in | |
// manual send test | |
NSLog("starting sending test on a serial queue ...") | |
var queue: DispatchQueue? | |
let uuid = NSUUID().uuidString | |
let queueLabel = "swiftybeaver-queue-" + uuid | |
queue = DispatchQueue(label: queueLabel, target: queue) | |
NSLog("using dispatch queue \(queueLabel)") | |
let url = URL(string: "https://api.swiftybeaver.com/ping")! | |
let operationQueue = OperationQueue() | |
operationQueue.underlyingQueue = queue | |
NSLog("setup operation queue") | |
let session = URLSession(configuration: URLSessionConfiguration.default, delegate: nil, delegateQueue: operationQueue) | |
NSLog("URL session is using operation queue") | |
print("sending to \(url) ...") | |
session.dataTask(with: url) { (data, response, error) in | |
NSLog("received response") // NEVER CALLED! | |
}.resume() | |
return "welcome" // executed before callback | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment