I've been trying to add values to my data array, however I came across the problem, that it goes to data.push() earlier than the value from getValue function is returned. I've been trying to fix it, but couldn't come up with the solution and feel kind of lost here.
this is the function where I form data:
formData() {
for (const id of Object.keys(this.cols)) {
let p= cols[id];
switch (p.name) {
case 'a':
this.temp.name= this.addValue(m, p);
break;
case 'b':
let idx = 1;
let surname= this.getValue(idx);
break;
}
}
this.data.push(this.temp);
});
and this is how my getValue function looks like:
getValue(id: string) {
let url = this.baseUrl + '/streams/' + id+ '/data?';
url += 'from=' + from + '&';
url += 'to=' + to;
this.http.get(url).map(r => r.json()).subscribe((response: any) => {
if (response.data.values[0] !== null && response.data.values[0] !== undefined) {
return response.data.values[0];
}
}, (error: any) => {
return null;
});
how can I avoid pushing data to array before that value is actually there?
formData()but to provide an answer you would have to show the full code offormData()(you don't seem to). Inthis.addValue(m, p)where ismcoming from? Inlet surname= this.getValue(idx);what are you actually doing with the returned value?surnameis never used!for (const id of Object.keys(this.cols))could befor (const p of Object.values(this.cols))if you only ever use theidto get a value fromthis.cols.