Skip to content

๐Ÿ”„ Swift utility class designed to convert callback-based asynchronous methods into the modern async/await pattern.

License

Notifications You must be signed in to change notification settings

mesqueeb/Asyncify

Folders and files

NameName
Last commit message
Last commit date

Latest commit

ย 

History

27 Commits
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 

Repository files navigation

Asyncify ๐Ÿ”„

.package(url: "https://github.com/mesqueeb/Asyncify", from: "0.0.9")

Asyncify is a utility class designed to convert callback-based asynchronous methods into Swift's async/await pattern.

This class is useful for adapting existing asynchronous code that uses callback functions / completion handlers to the newer async/await syntax in Swift, simplifying concurrency management in your codebase.

Usage

Suppose you have a function that performs an asynchronous operation using a completion handler to deliver its result. You can use Asyncify to wrap this function and call it using Swift's async/await syntax.

Example:

// Example asynchronous function using a completion handler
func fetchUserDataWithHandler(completion: @escaping (Result<UserData, Error>) -> Void) {
    // ... your function implementation
}

// Using Asyncify to wrap the asynchronous function
let userDataConverter = Asyncify<UserData>()

// Converting the callback-based function into an async function
func fetchUserData() async throws -> UserData {
    try await userDataConverter.performOperation { completion in
        fetchUserDataWithHandler(completion: completion)
    }
}

// Usage
Task {
    do {
        let userData = try await fetchUserData()
        print("Fetched user data: \(userData)")
    } catch {
        print("Failed to fetch user data: \(error)")
    }
}

This example demonstrates how Asyncify can be used to adapt a traditional callback-based function (fetchUserData) into a modern async/await pattern (getUserDataAsync), making it easier to use within Swift's concurrency model.

Documentation

See the documentation for more info.