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

Nested requests and conditions #26

Closed
Alpha0YZ opened this issue Aug 5, 2018 · 2 comments
Closed

Nested requests and conditions #26

Alpha0YZ opened this issue Aug 5, 2018 · 2 comments
Labels

Comments

@Alpha0YZ
Copy link

Alpha0YZ commented Aug 5, 2018

Hi,

I have a problem with nested requests. I would like to put conditions in .Then() function but I can't manage to do it. Here is my code:

RestClient.Get(GameManager.SERVER_URL + "/users?login=" + loginUser)
                .Then(res =>
                {
                    IPromise<ResponseHelper> post = null;

                    bool isAvailable = bool.Parse(res.Text);
                    if (isAvailable)
                    {
                        post = RestClient.Post(GameManager.SERVER_URL + "/users", new User { Login = loginUser });
                    }
                    else
                    {
                        Debug.Log("login taken");
                        LoginMessage.color = Color.red;
                    }
                    return post;
                })
                .Then(res =>
                {
                    bool isCreated = bool.Parse(res.Text);
                     ...
                }, 
                rejected =>
                {
                })
                .Catch(err =>
                {
                    GameManager.ShowNetworkError();
                    Debug.Log(err);
                })
                .Finally(() => PopupManager.HidePrefabPopup("LoadingPopup"));

I think it crashes because of post variable which is null when we enter the "else" condition.
Can you help me to use conditions with your project? Thanks in advance

@jdnichollsc
Copy link
Member

jdnichollsc commented Aug 6, 2018

@yzobairi you need to learn more about the promises library, check here https://github.com/Real-Serious-Games/C-Sharp-Promise#promises-that-are-already-resolvedrejected
Promise<ResponseHelper>.Resolved(null)

@jdnichollsc
Copy link
Member

@yzobairi any good comment from the Unity Asset Store would be really appreciated :)

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

No branches or pull requests

2 participants