Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Select an option

  • Save omidgolparvar/8c621a10318c67ea09af6720c45167a9 to your computer and use it in GitHub Desktop.

Select an option

Save omidgolparvar/8c621a10318c67ea09af6720c45167a9 to your computer and use it in GitHub Desktop.
/// https://www.donnywals.com/swift-concurrencys-taskgroup-explained/
///
/// When an error is thrown from the closure provided to withThrowingTaskGroup, the task group will fail with that error.
/// Before this error is thrown, the task group will mark any unfinished tasks as cancelled to allow them to stop
/// executing work as soon as possible in order to comply with Swift Concurrency's cooperative cancellation.
/// Once all tasks have completed (either by finishing their work or throwing an error), the task group will throw its error and complete.
///
/// ...
///
/// The core idea behind structured concurrency is that a task cannot outlive the scope of its parent task. And similarily,
/// no `TaskGroup` child task may outlive the scope of the `withTaskGroup` closure. This is achieved by implicitly
/// awaiting on all tasks to complete before returning from the closure you pass to `withTaskGroup`.
///
/// Once the task that manages the group throws an error, the scope of the task group has completed. If we still have running tasks
/// at that time, the tasks would outlive their group which isn't allowed. For that reason, the task group will first wait for all of its tasks
/// to either complete or throw a cancellation error before throwing its own error and exitting its scope.
///
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment