Skip to content

Instantly share code, notes, and snippets.

@christianselig
Created April 28, 2022 19:35
Show Gist options
  • Save christianselig/9ec53b180668c12b7f0a94574357e75e to your computer and use it in GitHub Desktop.
Save christianselig/9ec53b180668c12b7f0a94574357e75e to your computer and use it in GitHub Desktop.
Using Swift concurrency async await within a checked continuation
func doACoolAsyncThing() async -> Bool {
return withCheckedContinuation { continuation in
SomeAPI.oldSchoolContinuationHandler { result in
if result {
// Yay, success! But before we return at the parent level,
// check one more step, like ensuring iOS notification permissions
// are enabled
Task {
// Whoa! Async await inside a continuation. Allowed?
// (Assume this step is somewhat expensive and ONLY relevant
// if the previous step's result is true)
let finalStep = await isThe50000thDigitOfPi9()
continuation.returning(finalStep)
}
} else {
continuation.returning(false)
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment