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 { | |
try Task.checkCancellation() | |
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 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
func getUser() async throws -> User { | |
try await Task.sleep(nanoseconds: 1_000_000) | |
let task = Task(priority: .high) { () -> Address in | |
try await Task.sleep(nanoseconds: 1_000_000) | |
return Address(street: "FooStreet") | |
} | |
let address = try await task.value | |
return User(name: "Max", address: address) | |
} |
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(priority: .medium) { () -> String in | |
let text = try await requestTextFromServer() | |
return text | |
} |
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 { () -> String in | |
let text = try await requestTextFromServer() | |
return text | |
} | |
let result = try await task.result | |
switch result { | |
case let .success(value): | |
print(value) | |
case let .failure(error): |
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 { () -> String in | |
let text = try await requestTextFromServer() | |
return text | |
} | |
let newValue = try await task.value // Will only return if the Task did not throw |
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
// Will immediatly start running after creation | |
let task = Task { () -> String in | |
let text = try await requestTextFromServer() | |
return text | |
} |
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
struct ContentView: View { | |
private let formatter = { | |
let formatter = DateFormatter() | |
formatter.dateStyle = .none | |
formatter.timeStyle = .medium | |
return formatter | |
}() | |
private let timerSequence = Timer.publish(every: 1, | |
tolerance: 1, |
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 url = URL(string:"https://www.sebastianboldt.com") | |
let pollingSequence = URLPollingSequence(url: url!, delay: 2) | |
do { | |
for try await item in pollingSequence { | |
guard let data = item else { | |
continue | |
} | |
print(String(data: data, encoding: String.Encoding.utf8) ?? "Not decodable") | |
} | |
} catch { |