Swift 6.2 introduces immediate tasks via SE-472, allowing tasks to start synchronously on the caller's execution context rather than being scheduled for later. Task.immediate runs synchronously until the first actual suspension point, making it useful for calling async code from synchronous actor-isolated functions while preserving state update ordering. The post covers Task.immediate vs regular Task, actor isolation with immediate tasks, the risk of 'overhang' (blocking the caller executor with expensive synchronous work), Task.immediateDetached, and addImmediateTask in task groups. Key advice: use immediate tasks sparingly, only when the synchronous portion is known to be cheap.

8m read timeFrom avanderlee.com
Post cover image
Table of contents
Starting a task immediatelyCalling async code from synchronous actor codeRequesting a specific actorAvoiding overhangImmediate detached tasksImmediate tasks in task groups

Sort: