Promise is a simple Swift framework that works with iOS, MacOS and Linux. Promise provides a simple way for synchronization. The purpose of this framework is to provide a simple lightweight implementation of Promises for Swift. If you are looking for a more robust implementation then we recommend Google's Promises which are very robust and fairly light weight. This framework was built to be included in iOS PowerTools. A benefit this framework brings is that the entirety of the implementation is very lightweight and if desired can be added to a project without any dependency managers making the footprint smaller.
A very lightweight way to use this is to navigate to the Sources
directory and add all of the files from there to your project. This however does come with a caveat that the code will not be updated automatically in case of new versions. For a dependency manager approach follow one of the following methods.
As of Xcode 11 SPM integrates nicely with Xcode. This means that installing dependencies with Xcode support is super easy. To add the dependency using Swift Package Manager do the following steps:
- Select the desired project and choose the
Swift Packages
tab of your project. - Tap on the + button.
- Enter
https://github.com/sanderrouk/Promise-Swift
onto the search bar and click next. - Choose the
Version
option leaving the selection onUp to Next Major
option. - Click Next.
- Click Finish.
- Either create a separate file for it or add
import Promise
in the file where you want to use it.
- Add
github "sanderrouk/Promise-Swift" ~> 1.0.0
project to your Cartfile. - Run
$ carthage update
- Do the additional steps required for carthage frameworks.
- Either create a separate file for it or add
import PinKit
in the file where you want to use it.
Promise works just like any other promises framework making the syntax very familiar.
let promise = Promise<String>()
.always {
print("Always called")
}
promise.cancellationHandler = self
let secondPromise = promise.then({ string -> Bool in
throw GenericError.random
print(string)
return true
})
let thirdPromise = secondPromise.then({
return $0 ? "Hell yes" : "Hell nah"
})
thirdPromise.then({
print($0)
})
thirdPromise.cancel()
/// These two can't be called on the same promise
promise.resolve(value: "Initial value")
// promise.reject(error: GenericError.random)
promise.catch({ _ in
print("First promise catch")
})
secondPromise.catch({ _ in
print("Second promise catch")
})
thirdPromise.catch({ _ in
print("Third promise catch")
})
let exampleJson =
"""
{
"value": "Some string"
}
""".data(using: .utf8)!
Promise { fulfill, reject in
do {
let object = try JSONDecoder().decode(SomeDecodable.self, from: exampleJson)
fulfill(object.value)
} catch {
reject(error)
}
}
.then({ print($0) })
.catch({ print($0.localizedDescription) })
.always {
print("Called always on standalone function")
}
The project is under the MIT licence meaning you can use this project however you want.