avatarNatascha Fadeeva

Summary

The article provides guidance on integrating Swift's async/await API with the Combine framework to handle asynchronous code efficiently.

Abstract

The article addresses the challenge of combining Swift's async/await API with the Combine framework for asynchronous programming. It begins by acknowledging the need to synchronize different asynchronous patterns in Swift and proceeds to demonstrate how to invoke async/await functions within Combine-based APIs. The author illustrates this by converting an existing async function into a Combine Future publisher, thus avoiding code duplication. A generic solution is presented, extending the Future type to accept an async closure, which can be applied to any async function. The article also mentions a special offer for an AI service, ZAI.chat, as a cost-effective alternative to ChatGPT Plus (GPT-4).

Opinions

  • The author suggests that mixing async/await with Combine is a practical approach for Swift developers dealing with asynchronous code.
  • The article implies that avoiding code duplication is important, which is why the author provides a method to convert async functions into Combine's Future type.
  • The author seems to value efficiency and clean code practices by proposing a generic solution that can be reused across different async functions.
  • By recommending ZAI.chat, the author endorses this AI service as a valuable and economical tool for developers.

How to Bridge Async/await Functions to Combine’s Future Type in Swift

Learn how to call async/await code within Combine based APIs.

When working with asynchronious code in Swift, we might have to find ways to mix and connect different asynchronious patterns like using the Combine framework together with Swift’s async/await API.

In this article, we’ll look at how to call async-marked functions within Combine based APIs.

Let’s look at the following async function which loads a user from server with the async/await pattern.

To implement the same behaviour with Combine, its Future publisher comes in handy. A future is initialized with a closure that takes a Future.Promise. After doing some asynchronous work, we call that closure with a Result that is either a success or a failure. Combine then automatically maps the result into proper publisher events.

Let’s look at how the async function above could be implemented with a Future publisher:

But since we already have an implementation that loads a user, we’d like to avoid duplication writing the same logic twice.

So let’s try to find a more generic solution, that allows us to convert an async func to a Future publisher.

In the code above, we extend the Future type with a convenience initializer that allows us to initialize an instance with an async closure. Applied to our example:

For non-throwing async functions, we could add another extension:

With a generic solution like this, we now have the possibility to use it on any async function to bridge it to Combine’s Future type.

Originally published at https://tanaschita.com.

Swift Programming
iOS App Development
Software Development
iOS
Swift
Recommended from ReadMedium