In my Angular application, I am encountering a problem in the following scenario.
There are three crucial files:
MainCtrl.js
Upon loading the application, the init()
function in MainCtrl.js is triggered, which then calls the flowService as shown below:
flowService.configureSteps();
This call to the flowService initiates a server request to fetch data.
FlowService.js
function configureSteps() {
salesService.getSalesOrder().then(function(response) {
salesService.setSalesOrder(response);
});
}
The response received from the server is stored in the salesOrder
variable within the service for later retrieval.
SubCtrl.js
Within the initialization of SubCtrl, an attempt is made to access the saved salesOrder from the service.
function init() {
salesService.getSalesOrder();
}
Issue
When accessing the URL like this:
http://www.myapp.com/review#/subpage
- MainCtrl is invoked
- The service is called for data and a server request is initiated
- Due to the asynchronous nature of the call, control passes to SubCtrl before the data is retrieved
- As a result, an attempt to retrieve the salesOrder that hasn't been fetched yet leads to an issue
How can this process be made synchronous?