Swiftpack.co -  avreolko/VCPromises as Swift Package
Swiftpack.co is a collection of thousands of indexed Swift packages. Search packages.
avreolko/VCPromises
Promises pattern implementation
.package(url: "https://github.com/avreolko/VCPromises.git", from: "2.0.1")

Tests

VCPromises

A basic implementation of the Promise pattern in Swift

Installation

Install with SPM 📦

Features

  • then, catch, finally, thenMap, thenFlatMap, zip
  • flatten() global function for promises of the same type
  • Queuing
// you can define your work on some queue
// and then handle result on main
let work: Promise<Int>.Work = { fulfill, _ in
    do {
        let result = try someWork()
        fulfill(result)
    } catch {
        reject($0)
    }
}

Promise(work, on: .global(qos: .userInitiated))
.then(on: .main) { _ in }
  • Internal error handling
let promise = Promise<Int>()
promise.fulfill(1)

promise
    // all actions that you submit to then and map can throw
    .thenMap { _ in throw SomeError.foo }
    .catch { /* handle SomeError.foo */ }
  • Smart error catch
enum Complex: Error, Equatable {

    enum Reason {
        case parsingFailed
        case serviceError
    }

    case httpError(Int)
    case otherError(Reason)
}

let promise = Promise<Void>(error: ComplexError.httpError(500))

promise
    .catch(ComplexError.httpError(500)) { /* executes on .http(500) */ }
    .catch(ComplexError.other(.serviceError)) { /* executes on .other(.serviceError) */ }
    .catch(ComplexError.self) { /* executes on all Complex errors */ }
    .catch{ error in /* executes on all errors */ }

Usage

See /Tests for usage examples

License

This project is released under the MIT license.

GitHub

link
Stars: 0
Last commit: 3 days ago

Ad: Job Offers

iOS Software Engineer @ Perry Street Software
Perry Street Software is Jack’d and SCRUFF. We are two of the world’s largest gay, bi, trans and queer social dating apps on iOS and Android. Our brands reach more than 20 million members worldwide so members can connect, meet and express themselves on a platform that prioritizes privacy and security. We invest heavily into SwiftUI and using Swift Packages to modularize the codebase.

Swiftpack is being maintained by Petr Pavlik | @ptrpavlik | @swiftpackco | API