Skip to main content
An asynchronous initializer in Swift allows a type to suspend its instantiation process while awaiting the completion of asynchronous operations. By marking an init declaration with the async keyword, the initializer integrates directly into Swift’s structured concurrency model, enabling the resolution of stored properties through non-blocking, concurrent tasks.

Syntax

The async keyword is placed immediately after the initializer’s parameter list. If the initializer can also throw errors, async must precede the throws keyword.

Invocation

Because the initializer contains potential suspension points, instantiating the type requires the await keyword and must occur within an asynchronous context (such as a Task, an asynchronous function, or another asynchronous initializer).

Technical Mechanics and Compiler Rules

1. Definite Initialization and Suspension Points Swift’s definite initialization rules apply to async initializers, but the compiler permits suspension points (await) before all stored properties are fully initialized. The compiler enforces memory safety by ensuring that self cannot escape or have its instance methods called until Phase 1 of initialization (where all stored properties have an initial value) is complete, even across multiple suspension points. To compute values before Phase 1 completes, static methods or global functions must be used.
2. Actor Isolation When an actor defines an asynchronous initializer, the initialization process interacts specifically with the actor’s isolation domain.
  • Prior to the completion of Phase 1 initialization, the async initializer is treated as nonisolated.
  • Once all stored properties are initialized, the remainder of the initializer implicitly transitions to being isolated on the actor’s executor. This prevents data races if the initializer continues to mutate actor state after the initial allocation.
3. Delegation (self.init and super.init) Asynchronous initializers can delegate to other initializers.
  • A synchronous initializer cannot delegate to an asynchronous initializer.
  • An asynchronous initializer can delegate to either a synchronous or an asynchronous initializer. If delegating to an asynchronous initializer, the delegation call must be prefixed with await.
Tired of Poor Swift Skills? Fix That With Deep Grasping!Learn More