Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to convert a function with complete handler to async/await #55

Open
zhouhao27 opened this issue Jan 22, 2021 · 1 comment
Open

How to convert a function with complete handler to async/await #55

zhouhao27 opened this issue Jan 22, 2021 · 1 comment

Comments

@zhouhao27
Copy link

zhouhao27 commented Jan 22, 2021

I have a function:

func find(name: String, complete: (([Result])->Void))

I want to convert to a function something like this:

func find(name: String) -> Promise<Element?> {
        find(name: name) {
            (result) in
            switch result {
            case .success(let elements):
                if let element = elements {
                    // ??? How to return the element
                }
            case .failure(let error):
                print(error)
                // ??? How to return nil
            }
        }
}

The example is really simple one. Can AwaitKit used for the case like my example?

@HideOnBushTuT
Copy link

    
    func find(name: String) -> Promise<Element?> {
        Promise { seal in
            find(name: name) {
                (result) in
                switch result {
                case .success(let elements):
                    if let element = elements {
                        // ??? How to return the element
                        seal.fulfill(element)
                    } else {
                        seal.fulfill(nil)
                    }
                case .failure(let error):
                    print(error)
                    // ??? How to return nil
                    seal.fulfill(nil)
                }
            }
        }
    }

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants