google / promises

Promises is a modern framework that provides a synchronization construct for Swift and Objective-C.
Apache License 2.0
3.79k stars 292 forks source link

Why won't Promise { } fire? #149

Open mreaybeaton opened 4 years ago

mreaybeaton commented 4 years ago

If I have the following code

Promise { 
    statement1
}.then {
    ...
}.then {...}

statement1 doesn't fire, however if I have:

statement1.then{
    ...
}.then{...}

it does fire. Any ideas why that is?

ykjchen commented 4 years ago

Hey @mickeysox could you elaborate on what statement1 is? It looks like it could be a Promise, but I can't be sure just from the examples given. Thanks.

mreaybeaton commented 4 years ago

Hi @ykjchen

Here is my actual code:

@objc fileprivate func handleLogin() {
    let hud = JGProgressHUD(style: .dark)
    hud.textLabel.text = "Logging in"
    hud.show(in: self.view)
    if let username = phoneNumberTextField.text {
        Promise {
            self.viewModel.services.signIn(username: username)
        }.then { _ in
            self.viewModel.services.getUserAttributes()
        }.then { user in
            if let delegate  = self.delegate {
                delegate.update(user: user)
                hud.dismiss(animated: true)
                self.dismiss(animated: true)
            }
        }.catch { error in
            let registerController = RegisterController(viewModel: self.viewModel)
            registerController.username = username
            registerController.delegate = self.delegate
            hud.dismiss(animated: true)
            self.navigationController?.pushViewController(registerController, animated: true)
        }
    }
}

The definition of signIn is func signIn(username: String) -> Promise<AuthSignInResult>

The signIn method doesn't fire, but the "then" does. Changing this to

    if let username = phoneNumberTextField.text {
        self.viewModel.services.signIn(username: username)
        .then { _ in
        ...

does fire.

Thanks.

nsoojin commented 4 years ago

Try declaring the generic type

Promise<Void> {
  self.viewModel.services.signIn(username: username)
}...