freshOS / Then

:clapper: Tame async code with battle-tested promises
MIT License
993 stars 77 forks source link

Resolve promise on main thread #58

Closed diegoventura closed 4 years ago

diegoventura commented 5 years ago

👋

Is there a way to resolve a promise on the main thread?

I saw a resolveOnMainThread() on the readme, but couldn't find any reference to it in the source code.

Thanks

nodediggity commented 4 years ago

You could try -

extension Promise {

    public func resolveOnMainThread() -> Promise<T> {
        return Promise<T> { resolve, reject, progress in
            self.progress { p in
                DispatchQueue.main.async {
                    progress(p)
                }
            }
            self.registerThen { t in
                DispatchQueue.main.async {
                    resolve(t)
                }
            }
            self.onError { e in
                DispatchQueue.main.async {
                    reject(e)
                }
            }
        }
    }
}
s4cha commented 4 years ago

@diegoventura Indeed this is implemented in the ws library https://github.com/freshOS/ws. You can easily implement it with the code above shared by @nodediggity :)

s4cha commented 4 years ago

@@diegoventura Did that work out for you? can we close this?

diegoventura commented 4 years ago

Yes! Thanks and sorry for late response