Contents

init(name:executorPreference:priority:operation:)

Runs the given throwing operation asynchronously as part of a new unstructured top-level task.

Declaration

@discardableResult init(name: String? = nil, executorPreference taskExecutor: (any TaskExecutor)?, priority: TaskPriority? = nil, operation: sending @escaping () async throws -> Success)

Parameters

  • name:

    Human readable name of the task.

  • taskExecutor:

    The task executor that the child task should be started on and keep using. Explicitly passing nil as the executor preference is equivalent to no preference, and effectively means to inherit the outer context’s executor preference. You can also pass the Globalconcurrentexecutor global executor explicitly.

  • priority:

    The priority of the operation task.

  • operation:

    The operation to perform.

Return Value

A reference to the task.

Discussion

If the operation throws an error, it is caught by the Task and will be rethrown only when the task’s value is awaited. Take care to not accidentally dismiss errors by not awaiting on the task’s resulting value.

You need to keep a reference to the task if you want to cancel it by calling the Task.cancel() method. Discarding your reference to a task doesn’t implicitly cancel that task, it only makes it impossible for you to explicitly cancel the task.

See Also

Creating a Task