# Use of Async in sync? And their differences?

**URL:** <https://forums.swift.org/t/use-of-async-in-sync-and-their-differences/70751>\
**Category:** Using Swift\
**Tags:** concurrency\
**Created:** [March 18, 2024, 1:16pm UTC](https://forums.swift.org/t/use-of-async-in-sync-and-their-differences/70751 "2024-03-18T13:16:12Z")\
**Posts on this page:** 1\
**Showing post:** 13

<div class="post-metadata">

**Author:** ![nonameplum](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/nonameplum/32/6450_2.png) [@nonameplum](https://forums.swift.org/u/nonameplum)\
**Post date:** [March 19, 2024, 10:56am UTC](https://forums.swift.org/t/use-of-async-in-sync-and-their-differences/70751/13 "2024-03-19T10:56:22Z")

</div>

Using semaphore to force to wait for the `Task` completion is very risky and I wouldn't use it in the production code.

There is a great blog post about this topic in detail [Swift Concurrency Waits for No One](https://saagarjha.com/blog/2023/12/22/swift-concurrency-waits-for-no-one/)

Stick to other well-known tools for that.

For the `Button` action case. Personally I prefer to push as long a possible up the chain the need of spinning unstructured `Task`. In this case I would rather make a view model with the `async` function and spin a task from the `View`

```swift
Button {
    Task {
        await viewModel.buttonTapped()
    }
}

```

If you need to prevent the re-entrancy you might handle it either by making a guard in  
`viewModel` (e.g. checking the state or by keeping a reference to a task)

You might also create a custom `AsyncButton` reusable component that would have built-in reentrancy prevention + async actions support.

I agree that SwiftUI didn't encounter proper adoption of Swift Concurrency (besides the `task` modifier), leading to many (sometimes improper) solutions to the lack of handling async/await by default.

Testing unstructured `Task` is also a non-trivial [topic](https://forums.swift.org/t/reliably-testing-code-that-adopts-swift-concurrency/57304)

---

_[View the full topic](https://forums.swift.org/t/use-of-async-in-sync-and-their-differences/70751)._
