Swiftpack.co - avreolko/VCPromises as Swift Package

Swiftpack.co is a collection of thousands of indexed Swift packages. Search packages.
See all packages published by avreolko.
avreolko/VCPromises 2.3.1
Promises pattern implementation
⭐️ 0
🕓 2 years ago
iOS
.package(url: "https://github.com/avreolko/VCPromises.git", from: "2.3.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: 2 years ago
Advertisement: IndiePitcher.com - Cold Email Software for Startups

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