I'm struggling with running functions sequentially in my Angular 2/4 project. I have a retrieveData()
function that fetches data from a service and stores it in an array. Then, there's a displayData()
function that uses this data to create a chart. However, when I try to run them one after the other, like this:
function(){
this.retrieveData();
this.displayData();
}
The issue is that the displayData()
function runs before retrieveData()
, causing problems with displaying the graph correctly.
I've considered using async.waterfall
from the async
library but faced difficulties importing it into my project. The console gave me this error:
Uncaught Error: Unexpected value 'waterfall' imported by the module 'AppModule'. Please add a @NgModule annotation.
I prefer not to use Promises or Observables as they require return values from the initial function to pass on to the next ones. Although I tried using setTimeout()
to manage sequential execution, I worry about its reliability.
Any suggestions on effectively utilizing async
with Angular 2/4 or any alternative methods for making functions wait without relying on return promises?
UPDATE
Apologies for the confusion earlier. Here's a more complete snippet of my code. I'm new to Angular and TypeScript, especially when it comes to asynchronous programming techniques.
Below is my attempt at implementing promises in the retrieveAllData()
method. It compiles and runs without errors, but the functions still execute asynchronously, with refreshAllCharts()
occurring before retrieveAllData()
. Is there a flaw in how I'm handling promises?