Skip to content

Syntax sugar - Saving Previous Results #1126

@carlos-chaguendo

Description

@carlos-chaguendo

@mxcl What do you think of this syntax?

firstly {
    login()
}.retainValue { username in
    fetch(avatar: username)
}.retainValue { username, image in
    resize(image)
}.then { username, original, resized in
    …
}

you might consider adding the following functions

    public func retainValue<U: Thenable>(on: DispatchQueue? = conf.Q.map, flags: DispatchWorkItemFlags? = nil, _ body: @escaping(Self.T) throws -> U) -> Promise<(T, U.T)> {
        return then(on: on, flags: flags) { result in
            try body(result).map(on: on, flags: flags) { (result, $0) }
        }
    }
    
    /// tuple 2
    public func retainValue<U: Thenable, V, W>(on: DispatchQueue? = conf.Q.map, flags: DispatchWorkItemFlags? = nil, _ body: @escaping(T) throws -> U) -> Promise<(V, W, U.T)> where T == (V, W) {
        return then(on: on, flags: flags) { result in
            try body(result).map(on: on, flags: flags) { (result.0, result.1, $0) }
        }
    }
    
    /// tuple 3
    public func retainValue<U: Thenable, V, W, X>(on: DispatchQueue? = conf.Q.map, flags: DispatchWorkItemFlags? = nil, _ body: @escaping(T) throws -> U) -> Promise<(V, W, X, U.T)> where T == (V, W, X) {
        return then(on: on, flags: flags) { result in
            try body(result).map(on: on, flags: flags) { (result.0, result.1, result.2, $0) }
        }
    }

Xcode 11.2.1
PromiseKit 6

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions