Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/onmyway133/then

:runner: Promise A+ in Swift
https://github.com/onmyway133/then

ios promise swift

Last synced: 3 months ago
JSON representation

:runner: Promise A+ in Swift

Awesome Lists containing this project

README

        

# Then

❤️ Support my app ❤️

- [Push Hero - pure Swift native macOS application to test push notifications](https://www.producthunt.com/posts/push-hero-2)
- [PastePal - Pasteboard, note and shortcut manager](https://www.producthunt.com/posts/pastepal)
- [Frame recorder - Recorder gif and video with frame](https://www.producthunt.com/posts/frame-recorder)
- [Alias - App and file shortcut manager](https://www.producthunt.com/posts/alias-shortcut-manager)
- [Other apps](https://onmyway133.github.io/projects/)

❤️❤️😇😍🤘❤️❤️


Promises/A+ logo

[Promise A+](https://promisesaplus.com/) in Swift

## Usage

To run the example project, clone the repo, and run `pod install` from the Example directory first.

## Features

- Use Result to associate states with values
- Support protocol `Thenable`
- Support queue
- Transform Fulfilled value

### Then

```swift
let promise = Promise()

promise.then { result in
var condition = false
if case let .Fulfilled(value) = result where value == 10 {
condition = true
}

XCTAssert(condition)

return nil
} as Promise

promise.fulfill(value: 10)
```

### Chain

```swift
let promise = (Promise(result: .Fulfilled(value: "then"))
.then { result in
return result.map { value in
return value.characters.count
}
} as Promise)
.then { result in
return result.map { value in
return value * 2
}
} as Promise

promise.then { result in
if case let .Fulfilled(value) = result {
XCTAssert(value == 8)
} else {
XCTAssert(false)
}

return nil
} as Promise
}
```

### All

```swift
let promise1 = Promise(result: .Fulfilled(value: 1))
let promise2 = Promise(result: .Fulfilled(value: 2))
let promise3 = Promise(result: .Fulfilled(value: 3))

let final = Promise.all(promises: [promise1, promise2, promise3])

final.then { result in
if case let .Fulfilled(value) = result {
XCTAssert(value.count == 3)

XCTAssert(value.values.contains(1))
XCTAssert(value.values.contains(2))
XCTAssert(value.values.contains(3))

XCTAssert(value.keys.contains(promise1.key))
} else {
XCTAssert(false)
}

return nil
} as Promise
```

## Installation

Then is available through [CocoaPods](http://cocoapods.org). To install
it, simply add the following line to your Podfile:

```ruby
pod 'Then', git: 'https://github.com/onmyway133/Then'
```

## Author

Khoa Pham, [email protected]

## License

Then is available under the MIT license. See the LICENSE file for more info.