Skip to content

piresbruno/SPKit

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

20 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

SPKit is just another Swift promises framework, it prevents the callback hell and supports sync and async code.
It was made be to simple to use, read and maintain.
It works with Xcode 10.2 and Swift 5

How to install SPKit

You can install SPKit using carthage, or just drag the SPKit.swift file to your project.

How to use SPKit

Start the flow by calling SPKit.first, move to the next block by calling .resolve() and finish the flow by calling .complete() or .failure().

Example happy path

SPKit.first { (instance) in
	print("first code block")
	instance.resolve()
}.then { (instance, result) in  
	print("next code block")
    instance.complete()
}.onCompleted {(result) in
    print("COMPLETED")
}.onFailure {(error) in
    print("FAILURE")
}

Example happy path with sync and async

SPKit.first { (instance) in
	print("first code exec")
	instance.resolve()
}.then { (instance, result) in
    DispatchQueue.global().asyncAfter(deadline: .now() + .seconds(1)){
        print("async code")
        instance.resolve()
    }
}.then { (instance, result) in
    print("sync code")
    instance.resolve("finished stage 2")
}.then { (instance, result) in  
	print("using result value: \(result as? String ?? "")")
    instance.complete()
}.onCompleted {(result) in
    print("COMPLETED")
}.onFailure {(error) in
    print("FAILURE")
}

Example error path

SPKit.first { (instance) in
	print("first code exec")
	instance.resolve()
}.then { (instance, result) in
    print("sync code")
    instance.resolve("finished stage 2")
}.then { (instance, result) in
	print("using result value: \(result as? String ?? "")")
    instance.failure("ooppps")
}.onCompleted {(result) in
    print("COMPLETED")
}.onFailure {(error) in
    print("FAILURE")
}
Doubts or questions

Please open an issue.

Build Carthage compatible Swift version platforms

About

Simple Promise Kit for swift

Resources

License

Stars

Watchers

Forks

Packages

No packages published