Swiftpack.co -  chronicqazxc/WHPromise as Swift Package
Swiftpack.co is a collection of thousands of indexed Swift packages. Search packages.
chronicqazxc/WHPromise
Light weight Promise (Future) framework in iOS.
.package(url: "https://github.com/chronicqazxc/WHPromise.git", from: "0.0.2")

WHPromise

CI Status License Platform Platform Platform Platform

Light weight Promise (Future) framework in iOS.

How to use.

  1. Client initiate a promise instance.
  2. Subscribe then and reject events.
  3. Receive event.

Case 1. JSON Parser promise

    func jsonParser<Type: Codable>(data: Data) -> Promise<Type> {
        let promise = Promise<Type> { (fulfill, reject) in
            do {
                let decoder = JSONDecoder()
                let products = try decoder.decode(Type.self, from: data)
                fulfill(products)
            } catch {
                reject(error)
            }
        }
        return promise
    }

Case 2. Get product type from order id

    func getProductFromOrder(orderId: String) -> Promise<GroceryProduct> {
        let promise = Promise<GroceryProduct> { (fulfill, reject) in
            DispatchQueue.global().async {
                let string = """
                    {"name": "Banana",
                       "points": 200,
                       "description": "A banana grown in Ecuador."
                    }
                """
                let jsonData = string.data(using: .utf8)
                let jsonPromise = self.jsonParser(data: jsonData!) as Promise<GroceryProduct>
                jsonPromise.then { product in
                    fulfill(product)
                }.catch { error in
                    reject(error)
                }
            }
        }
        return promise
    }

Case 3. Get product from orderId

func testGetProduct() {
        let ex = expectation(description: "")
        let product = getProductFromOrder(orderId: "12345")
        product.then { product in
            ex.fulfill()
            XCTAssertEqual(product.name, "Banana")
            XCTAssertEqual(product.orderId, "12345")
        }.catch { error in
            XCTAssertFalse(false, error.localizedDescription)
        }

        wait(for: [ex], timeout: 10)
    }

Architecture diagram.

sequence

Sequence diagram.

sequence

Author:

Wayne Hsiao

GitHub

link
Stars: 0
Last commit: 4 weeks 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