Created
April 28, 2022 19:35
-
-
Save christianselig/9ec53b180668c12b7f0a94574357e75e to your computer and use it in GitHub Desktop.
Using Swift concurrency async await within a checked continuation
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
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