cormacmchale / ProfPractice

An ionic App
0 stars 0 forks source link

Getting the asynchronous functions to run correctly #12

Closed cormacmchale closed 5 years ago

cormacmchale commented 5 years ago

One major issue we had during development was understanding exactly how asynchronous functions work and how to handle them. This section will document what we have learned

cormacmchale commented 5 years ago

Possibly the biggest learning curve in our project was to understand how the app handled asynchronous function during deployment.

cormacmchale commented 5 years ago

To give a brief description of what we Initially understood about these functions, we knew obviously that they run in the background. i.e that when you make an asynchronous function call the app will go off some where else and await the result of the function call. However what took us a while to reach an understanding of was that all the results of these asynchronous functions must always be handled inside of the callback function. That is to say that if you have your own static methods that rely on values returned in a call back function then your own methods must be fired inside the callback function for a reliable result.

cormacmchale commented 5 years ago

https://stackoverflow.com/questions/748175/asynchronous-vs-synchronous-execution-what-does-it-really-mean

If you read through this thread there are some good answers and some terrible ones. Anyway the problem is not to understand the logic... It's to understand that if you wish to write a program or application that handles synchronous and asynchronous functions working in unison is that you can never predict if you are going to have the values you need in your synchronous function from an asynchronous result. What I mean by this is that all of your 'static' methods you have written will execute one after the other, line by line, no matter what. Its impossible to tell where you are going to be in execution at any point so if you have a reference to a variable that gets its value from a callback function then you may get to a point in code where a value is needed that is not there and this is guaranteed to produce bad results.

cormacmchale commented 5 years ago

This is how I was initially programming to avoid errors. If you follow the code you can see that the service returns an observable collection to the app. In map page we wanted all these Journeys just to display for the user. This wouldn't work because painting the markers on the map is a static method and would execute before the values were stored in this.markersToShow. How I fixed this before learning exactly what was happening was to make the user press a button to show the Journeys. Only after research did I come to understand that this is because waiting for the page to load and the user to press a button will always allow enough time for the database info to be retrieved. Or the user can try pressing the button again. 5 2 10

cormacmchale commented 5 years ago

Learning how to implemented a Firebase login was what prompted me to learn exactly what was happening with asynchronous function, In case I had to explain during the presentation of the app. 4

cormacmchale commented 5 years ago

Finally, you can see from the implementation of the map zoom and also the Gecoding on the way up to the database so we can retrieve an address to display the user that I had figured out programmatically how to handle this situations correctly. It has resulted in an over-all more rewarding experience for the user.

1 3