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 getDocuments(range: Range<Int>) async throws -> [Data] { | |
// 1. | |
let documents = try await withThrowingTaskGroup(of: Data.self, returning: [Data].self,body: { group -> [Data] in | |
// 2. | |
for i in range { | |
group.addTask(priority: .high, operation: { () -> Data in | |
return try await getDocument(fileName: "Document-\(i)") | |
}) | |
} | |
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
let task = Task { | |
for i in 0..<1_000_000_000_000_000 { | |
guard !Task.isCancelled else { | |
break | |
} | |
print(i) | |
} | |
} | |
task.cancel() |
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
let loadDataTask = Task { () -> Data? in | |
let url = URL(string: "https://www.sebastianboldt.com")! | |
let (data, _) = try await URLSession.shared.data(from: url) | |
return data | |
} | |
let result = try await loadDataTask.result | |
switch result { | |
case let .success(data): |
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
let loadDataTask = Task(priority: .medium) { () -> Data | |
let url = URL(string: "https://hws.dev/headlines.json")! | |
let (data, _) = try await URLSession.shared.data(from: url) | |
return data | |
} | |
let data = try await loadDataTask.value |
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
import UIKit | |
import _Concurrency | |
import PlaygroundSupport | |
PlaygroundPage.current.needsIndefiniteExecution = true | |
// 1. | |
struct URLPollingSequence: AsyncSequence, AsyncIteratorProtocol { | |
// 2. | |
typealias AsyncIterator = URLPollingSequence |
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 printData() async throws { | |
var iterator = getAsyncIterator() | |
if let item = try await iterator.next() { | |
print("The first item is \(item)") | |
} | |
while let item = try await iterator.next() { | |
print("\(item)") | |
} |
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 fetchItems() async throws { | |
for try await item in getAsyncSequence() { | |
print("Received item: \(item)") | |
} | |
} | |
try? await fetchItems() |
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
class DelegateWrapper: NSObject, ObservableObject, CLLocationManagerDelegate { | |
private var continuation: CheckedContinuation<Data?, Error>? | |
private let dataService = SomeClassThatReturnsData() | |
override init() { | |
super.init() | |
dataService.delegate = self | |
} | |
func requestData() async throws -> Data? { |
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
enum SomeError: Error { | |
case foo | |
} | |
func completionFunction(completion: @escaping ([Data], error: @escaping (Error) -> Void) -> Void) { | |
getData(complete: completion) | |
} | |
func asyncFunctionWrapper() async -> Data { | |
await withCheckedThrowingContinuation { continuation in |
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 getStatistics(for user: User) async throws -> (Statistics, Statistics) { | |
async let old = getOldUserStatistics(of: user) | |
async let new = getNewUserStatistics(of: user) | |
let awaitedOldStats = await old | |
let awaitedNewStats = await new | |
return (awaitedOldStats, awaitedNewStats) | |
} |