immediate(name:priority:executorPreference:operation:)
Create and immediately start running a new detached task in the context of the calling thread/task.
Declaration
@discardableResult static func immediate(name: String? = nil, priority: TaskPriority? = nil, executorPreference taskExecutor: consuming (any TaskExecutor)? = nil, operation: sending @escaping @isolated(any) () async throws -> Success) -> Task<Success, any Error>Parameters
- name:
The high-level human-readable name given for this task
- priority:
The priority of the task. Pass
nilto use the Basepriority of the current task (if there is one). - taskExecutor:
The task executor that the child task should be started on and keep using. Explicitly passing
nilas 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. - operation:
The operation to be run immediately upon entering the task.
Return Value
A reference to the unstructured task which may be awaited on.
Discussion
This function starts the created task on the calling context. The task will continue executing on the caller’s context until it suspends, and after suspension will resume on the adequate executor. For a nonisolated operation this means running on the global concurrent pool, and on an isolated operation it means the appropriate executor of that isolation context.
As indicated by the lack of async on this method, this method does not suspend, and instead takes over the calling task’s (thread’s) execution in a synchronous manner.
Other than the execution semantics discussed above, the created task is semantically equivalent to a task created using the Task/init initializer.