Reputation: 524
In my code below I would like would like to finish
this.storage.set ("location", JSON.stringify(lsd_info));
before proceeding to
this.driveTo();
I believe I should be using .then(). What I am wondering is there a simple solution to finish an event before proceeding.
getLoc() {
let a = this.data.lsd;
let b = this.data.sec;
let c = this.data.twp;
let d = this.data.rng;
let e = this.data.mrd;
this.http.get('https://www.reversegeocoder.com/api/v1/PrivateID/lsd/' + a + '-' + b + '-' + c + '-' + d + ' ' + e)
.map(res => res.json())
.subscribe (data => {
let lsd_info = {
latitude: data[0].response.lat,
longitude: data[0].response.lng,
};
let lsd_error = {error: data[0].response.err};
this.ErrorResponse = (JSON.stringify(data[0].response.err));
this.ErrorText = this.ErrorResponse.replace('[','').replace(']','');
this.storage.set ("location", JSON.stringify(lsd_info));
//finish storage.set before proceeding
this.driveTo();
},
err => {
console.log('error');
}
);
}
driveTo() {
this.navCtrl.push(Drive);
}
Or have two functions where on function finished before proceeding
i.e. getLoc() and then driveTo()
Upvotes: 0
Views: 704
Reputation: 2327
Your guess is correct. You should be using .then()
to put your code which should run once the ionic storage
has set the key-value pair, as ionic storage
returns a promise which resolves, when key-value has set. I have modified your code and the solution is simple enough.
getLoc() {
let a = this.data.lsd;
let b = this.data.sec;
let c = this.data.twp;
let d = this.data.rng;
let e = this.data.mrd;
this.http.get('https://www.reversegeocoder.com/api/v1/PrivateID/lsd/' + a +
'-' + b + '-' + c + '-' + d + ' ' + e)
.map(res => res.json())
.subscribe (data => {
let lsd_info = {
latitude: data[0].response.lat,
longitude: data[0].response.lng,
};
let lsd_error = {error: data[0].response.err};
this.ErrorResponse = (JSON.stringify(data[0].response.err));
this.ErrorText = this.ErrorResponse.replace('[','').replace(']','');
this.storage.set ("location", JSON.stringify(lsd_info)).then(
(value) => {
// storage.set finished
this.driveTo();
},
(reason) => {
console.log('Error occurred.');
console.warn(reason);
});
},
err => {
console.log('error');
}
);
}
driveTo() {
this.navCtrl.push(Drive);
}
Upvotes: 2