Is there a way to check if a Task is still running or has completed?

When you create a Task and keep a reference to it. Can one somehow check if the task has finished running or is still executing?

Not sure what's your goal is but if you do:

let task = Task {...}
_ = await task.value

On the next line in your code the task has finished (e.g. when value returns the task has completed). You can update a boolean value or something else or just call a function, etc..

Hi @icanzilb thanks for answering!
My idea is to have something like:

let task = Task { ... }

if task.isStillRunning {
   printf("Loading....")
} else {
   printf("Task is done!")
}

Then, consider writing it like this:

print("Loading...")
Task {
   ...
   print("Task is done!")
}
1 Like