Search code examples
swiftasync-awaitspawnswift5

TaskGroup `async` vs. `spawn`


Does anyone know, in the new Swift async/await stuff, is there any difference between TaskGroup async and spawn, or are they pure synonyms? (If they are synonyms, I rather like spawn better. async looks like we're opening an async block, and that's not what we're doing at all.)

https://developer.apple.com/documentation/swift/taskgroup/3814850-async

https://developer.apple.com/documentation/swift/taskgroup/3814884-spawn


Solution

  • SE-0304, tells us that spawn was renamed async as part of the second review:

    • TaskGroup.spawn and TaskGroup.spawnUnlessCancelled have been renamed to TaskGroup.async and TaskGroup.asyncUnlessCancelled which are to be their final names. This aligns the naming with the renamed async let as the word signifying creation of a child task.

    The portion in italics has subsequently been removed and the third review has renamed it again:

    • renamed TaskGroup.async and TaskGroup.asyncUnlessCancelled to TaskGroup.addTask and TaskGroup.addTaskUnlessCancelled. The fundamental behavior here is that we're adding a task to the group. add by itself does not suffice, because we aren't adding a value (accessible via next()), we are adding a task whose value will be accessible via next(). It also parallels the use of Task { ... } to create top-level tasks.