Skip to content

Instantly share code, notes, and snippets.

@olbrichj
Created March 21, 2018 11:50
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save olbrichj/c76e2f5933bfaf6c68632925f4f593e6 to your computer and use it in GitHub Desktop.
Save olbrichj/c76e2f5933bfaf6c68632925f4f593e6 to your computer and use it in GitHub Desktop.
func foo() -> (Promise<Void>, cancel: () -> Void) {
let task = Task(…)
var cancelme = false
let promise = Promise<Void> { seal in
task.completion = { value in
guard !cancelme else { reject(NSError.cancelledError) }
seal.fulfill(value)
}
task.start()
}
let cancel = {
cancelme = true
task.cancel()
}
return (promise, cancel)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment