title: Structured Concurrency difference between addTask and addTaskUnlessCancelled Swift
body: <p>I am have confusion between Calling <code>addTask()</code> and <code>addTaskUnlessCancelled</code>. By definition <code>addTask()</code> on your group will unconditionally add a new task to the <code>group</code></p>
  <pre class="lang-swift prettyprint-override"><code>func testCancellation() async {
      do {
          try await withThrowingTaskGroup(of: Void.self) { group -&gt; Void in
              group.addTaskUnlessCancelled {
                  print(&quot;added&quot;)
                  try await Task.sleep(nanoseconds: 1_000_000_000)
                  throw ExampleError.badURL
              }
              group.addTaskUnlessCancelled {
                  print(&quot;added&quot;)
                  try await Task.sleep(nanoseconds: 2_000_000_000)
                  print(&quot;Task is cancelled: \(Task.isCancelled)&quot;)
              }
  
              group.addTaskUnlessCancelled {
                  print(&quot;added&quot;)
                  try await Task.sleep(nanoseconds: 5_000_000_000)
                  print(&quot;Task is cancelled: \(Task.isCancelled)&quot;)
              }
              group.cancelAll()
              try await group.next()
  
          }
      } catch {
          print(&quot;Error thrown: \(error.localizedDescription)&quot;)
      }
  }
  </code></pre>
  <p>If you want to avoid adding tasks to a cancelled group, we have to use the <code>addTaskUnlessCancelled()</code> method. But even with <code>group.cancelAll()</code>, it is adding all Task to group. Then what's the difference here and returning value which return <code>true</code> only if <code>Task</code> throws some error?</p>