ClosureChain
public class ClosureChain
ClosureChain simplifies sequential async completion methods for Swift. It provides a familiar try-catch pattern for sequential async methods.
-
Failures that may be sent to the
See morecatch
block due to run-time errorsDeclaration
-
Every
ClosureChain
has a singleCatchHandler
associated with it. If any errors are thrown within a try-block, or fromClosureChain
itself due to usage errors, subsequent try-blocks will no longer be called and theCatchHandler
will receive the associatedError
.Declaration
Swift
public typealias CatchHandler = (Error) -> Void
-
Initialize a closure chain
Declaration
Swift
public init()
-
Execute a block. Each
try
block will be executed sequentially with another.Declaration
Swift
public func `try`(_ completion: @escaping (Link) throws -> Void)
Parameters
completion
Block to execute. This block must call either
Link.success()
orthrow
an error to continue theClosureChain
-
Execute a block. Each
try
block will be executed sequentially with another.Declaration
Swift
public func `try`<RequiredType>(_ completion: @escaping (_ param: RequiredType, Link) throws -> Void)
Parameters
completion
Block to execute. A parameter can be specified with type. This type must match exactly the type of the
.success()
call of a priortry
block. This block must call eitherLink.success()
orthrow
an error to continue theClosureChain
-
Error handler. This error handler is called if any try-block has thrown an error (or if
ClosureChain
throws an error). No subsequent try-blocks will be executed.Declaration
Swift
public func `catch`(_ completion: @escaping CatchHandler)
Parameters
completion
Error handler
-
This method must be called at some point after all try-blocks have been defined. No try-blocks will be executed otherwise.
Declaration
Swift
public func start()
-
See moreClosureChain
sequentially executes try-blocks, also known asLink
s. Links must receive exactly one of either a.success()
or a.throw()
to signfiy the completion of theLink.
Declaration
Swift
class Link