How to replace async code using completion handler with Future from Combine Framework

Spencer Feng
1 min readNov 8, 2021

In Swift, we can handle asynchronous events by using completion handlers, in which a caller provides a closure to execute once, after a potentially long-running task completes.

Let’s have a look at the code below. As you can see, get cached data is an asynchronous action and the completion handler is executed with the corresponding result either after we successfully get the cached items or fail to do so.

--

--