0

So here I will initialise my arrays with the call this.Reload.All() to go and get all the arrays I need to filter things later in the program. What happens is that all 4 the calls are made and then the 4 responses come back. This confuses & breaks the service because it cannot take in so many calls before giving back their responses. What I am looking for is a synchronous solution that would do something like this: call 1 - response 1 - call 2 - response 2 - call 3 - response 3 - call 4 - response 4. The reponse.dbX_response.view is just the way my JSON comes back from the server.

All of my code is working correctly, it is just the async that is breaking my calls. How can I fix this?

//Reload Reference Arrays
    RefVehicleTypes = [];
    RefClients = [];
    RefUsers = [];
    RefObjects = [];

//Reload Class:
    Reload = {
        DoCall : (pUrl, pArray, pResponse) => {
            this.HttpClient.get(pUrl).subscribe(
                (response: any) => {
                    this[pArray] = eval(pResponse);
                    console.log(pResponse + ' : ' + this[pArray]);
                }
            );   
        },
        All : () => {
            this.Reload.VehicleTypes();
            this.Reload.Clients();
            this.Reload.Users();
            this.Reload.Objects();
        },
        VehicleTypes : () => {
            this.Reload.DoCall(
                'http://...',
                'RefVehicleTypes',
                'response.dbVehicleType_response.view',
            );
        },
        Clients : () => {
            this.Reload.DoCall(
                'http://...',
                'RefClients',
                'response.dbClient_response.view',
            );
        },
        Users : () => {
            this.Reload.DoCall(
                'http://...',
                'RefUsers',
                'response.dbUser_response.view'
            );
        },
        Objects : () => {
            this.Reload.DoCall(
                'http://...',
                'RefObjects',
                'response.dbObject_response.view'
            );
        }
    }

NEW EDIT:

So I replaced the DoCall and All methods with this, but this still doesn't work. Check the screenshot below for the results.

DoCall : (pUrl, pArray, pResponse) => {
    return new Promise((resolve, reject) => {
        this.HttpClient.get(pUrl).subscribe(
            (response: any) => {
                console.log('##############################' + pArray)
                console.log(this[pArray]);
                this[pArray] = eval(pResponse);
                console.log(this[pArray]);
                resolve();
            }
        )
    });
},
All : async () => {
    await this.Reload.VehicleTypes();
    await this.Reload.Clients();
    await this.Reload.Users();
    await this.Reload.Objects();
},

Here are 3 screenshots of 3 refreshes. As you can see, it seems like the async All() function causes the arrays to be mixed around, which is ok. Not sure why but it is still somehow delaying the responses. The last one always works, which probably means the previous responses are being replaced by the next one coming in?

enter image description here

2
  • 1
    Return a promise in your DoCall method and chain the commands in your All method or use await Commented May 14, 2018 at 6:25
  • Fussel would you mind writing out a basic example of this? I am quite new to angular and promises are quite tricky Commented May 14, 2018 at 6:41

1 Answer 1

2

So, this is only written here, so the code is not tested, but something like this should work to make your calls synchronous.

Reload = {
    DoCall : (pUrl, pArray, pResponse) => {
        return new Promise((resolve, reject) => {
            this.HttpClient.get(pUrl).subscribe(
                (response: any) => {
                    this[pArray] = eval(pResponse);
                    console.log(pResponse + ' : ' + this[pArray]);

                    resolve();
                }
            );  
        }
    },
    All : () => {
        this.Reload.VehicleTypes().then(() => {
            return this.Reload.Clients();
        }).then(() => {
            return this.Reload.Users();
        }).then(() => {
            return this.Reload.Objects();
        }).then(() => {
            console.log("Everything reloaded");
        });
    },
    VehicleTypes : () => {
        return this.Reload.DoCall(
            'http://...',
            'RefVehicleTypes',
            'response.dbVehicleType_response.view',
        );
    },
    Clients : () => {
        return this.Reload.DoCall(
            'http://...',
            'RefClients',
            'response.dbClient_response.view',
        );
    },
    Users : () => {
        return this.Reload.DoCall(
            'http://...',
            'RefUsers',
            'response.dbUser_response.view'
        );
    },
    Objects : () => {
        return this.Reload.DoCall(
            'http://...',
            'RefObjects',
            'response.dbObject_response.view'
        );
    }
}

Alternatively in newer JS version you could use

All : async () => {
    await this.Reload.VehicleTypes();
    await this.Reload.Clients();
    await this.Reload.Users();
    await this.Reload.Objects();
},

while still using promises.

Sign up to request clarification or add additional context in comments.

9 Comments

So I tried your solution (thought I added async in the All() function otherwise the awaits give an error, is this wrong?). This is still not working though. Have a look at my edited question please :)
Do you also return the promise in your other methods?
Again I am very new so not sure, don't think so. This is the only place where I now use promises. I have exactly the code in my original question, except for the changed DoCall and All methods, that now look like the edit post
In my code there is also a return in all other methods aswell so that these return the promise to the All method where the await is used.
DUDE THANK YOU THIS WORKS! This is a problem I have been battling with for such a long time. Would you might explaining the "Alternatively in newer JS version you could use" part? If I use await here then the await words give an error? Maybe edit your post?
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.