# Avoid Multiple call to function or Task launch before they return

**URL:** <https://forums.swift.org/t/avoid-multiple-call-to-function-or-task-launch-before-they-return/65479>\
**Category:** Using Swift\
**Created:** [June 9, 2023, 11:32pm UTC](https://forums.swift.org/t/avoid-multiple-call-to-function-or-task-launch-before-they-return/65479 "2023-06-09T23:32:48Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![nicpro85](https://avatars.discourse-cdn.com/v4/letter/n/46a35a/32.png) [@nicpro85](https://forums.swift.org/u/nicpro85)\
**Post date:** [June 9, 2023, 11:32pm UTC](https://forums.swift.org/t/avoid-multiple-call-to-function-or-task-launch-before-they-return/65479/1 "2023-06-09T23:32:48Z")

</div>

I am looking for a more elegant way to protect a function from multiple call when the user press multiple time the button quickly in such situation:

```swift
    func didPressSignIn() {
        Task { await signInIfAllowed() }
    }
    
    @MainActor
    private func signInIfAllowed() async {
        guard !isSigningIn else {return}
        isSigningIn = true
        lastError = await signIn()
        isSigningIn = false
    }

```

---

<div class="post-metadata">

**Author:** ![tera](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/tera/32/38201_2.png) [@tera](https://forums.swift.org/u/tera)\
**Post date:** [June 9, 2023, 11:46pm UTC](https://forums.swift.org/t/avoid-multiple-call-to-function-or-task-launch-before-they-return/65479/2 "2023-06-09T23:46:10Z")

</div>

Consider signIn taking long time (e.g. 30 seconds)... You don't want the button to be visually enabled and clickable with nothing happening. The most proper way is to disable the button until such time user can attempt signing in again (e.g. sign in failed or user has signed out).

---

<div class="post-metadata">

**Author:** ![nicpro85](https://avatars.discourse-cdn.com/v4/letter/n/46a35a/32.png) [@nicpro85](https://forums.swift.org/u/nicpro85)\
**Post date:** [June 9, 2023, 11:54pm UTC](https://forums.swift.org/t/avoid-multiple-call-to-function-or-task-launch-before-they-return/65479/3 "2023-06-09T23:54:18Z")

</div>

Thank you for your answer. It is indeed already the case. The button disable state is linked to the isSigninIn. However I was wondering if here is a thing such as

`ProtectedTask { await function }`  
or something like  
`func signIn() async protected { ... }`

which would ditch any attempt to start the same code/task when one is still running.

or maybe the Task itself would be the isSigninIn state.

---

<div class="post-metadata">

**Author:** ![DevAndArtist](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/devandartist/32/287_2.png) [@DevAndArtist](https://forums.swift.org/u/DevAndArtist)\
**Post date:** [June 10, 2023, 4:40pm UTC](https://forums.swift.org/t/avoid-multiple-call-to-function-or-task-launch-before-they-return/65479/4 "2023-06-10T16:40:36Z")

</div>

The trick is simple. Store your task while it's active and remove it when it's done. If you somehow manage to call the synchronous function again then you will check if there is already a task running and ignore the invocation by simply returning and doing nothing.
