Callbacks can be effective but often lead to messy code. The introduction of async and await provides a streamlined and linear syntax for handling concurrent computations, resulting in cleaner and more readable code.
- Conversion of callback functions to async functions
- Improved code readability and cleanliness
private func delayAfter(delay: Int, completation: @escaping ((Int) -> Void)) {
let dispatchAfter = DispatchTimeInterval.seconds(delay)
DispatchQueue.main.asyncAfter(deadline: .now() + dispatchAfter) {
completation(delay)
}
}
private func delayAfterAsync(delay: Int) async -> Int {
return await withCheckedContinuation({ continuation in
delayAfter(delay: delay) { delay in
continuation.resume(returning: delay)
}
})
}
Please check the Code Here
Check the detailed article from Medium.
This part delves into the technique of invoking multiple tasks asynchronously and provides a performance comparison.
- Asynchronous task execution
- Performance evaluation and comparison
private func SynchronousTask() async throws {
let firstDelay = try await delayAfter(delay: 4) //1
print("First Delay \(firstDelay)") //2
let secondDelay = try await delayAfter(delay: 2) //3
print("Second Delay \(secondDelay)") //4
}
private func ASynchronousTask() async throws{
async let firstDelay = delayAfter(delay: 4) //1
async let secondDelay = delayAfter(delay: 2) //2
print("First Delay\(try await firstDelay)") //3
print("Second Delay\(try await secondDelay)") //4
}
Please check the Code Here
Check the detailed article from Medium.
This article focuses on the execution of a list of tasks. The article will also delve into the significance of task cancellation, particularly when a task encounters an error.
- Task Execution Loop
- Task Cancellation
func performTaskLoopsInCancelable() async throws {
var cancelledTaskDelays = [Int]()
for delay in delays {
do {
try Task.checkCancellation()
let d = try await delayAfter(delay: delay)
print(d)
}catch{
cancelledTaskDelays.append(delay)
}
}
print("Cancelled delays \(cancelledTaskDelays)")
}
Please check the Code Here
Check the detailed article from Medium.
In this installment, our focus shifts to the parallel execution of tasks, accomplished efficiently through the use of Task Groups.
- Exploring Parallel Execution
- Task Efficiency with Task Groups
func getProcessingTime(students: [Student]) async throws {
var processingTimeList: [Int: Int] = [:]
await withTaskGroup(of: Void.self, body: { group in
for student in students {
group.addTask {
do {
let processingTime = try await delayAfter(delay: student.processingTime)
print("processing Time \(processingTime) id \(student.id)")
processingTimeList[student.id] = processingTime
} catch {
print("error id \(student.id)")
}
}
}
})
}
Please check the Code Here
Check the detailed article from Medium.