0

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()

Suraj Rao
  • 29,388
  • 11
  • 94
  • 103
Thomas Degroot
  • 441
  • 1
  • 12
  • 32
  • What does `this.storage.set do`? – eko May 12 '17 at 04:47
  • @echonax It will set a key-value pair either in `SQLite` database of device or local storage a key-value pair in browser depending on the platform, the app is running in by using `ionic-native`, `Storage` plugin. – rmalviya May 12 '17 at 05:01
  • @echonax You can check ionic storage documentation [here](https://ionicframework.com/docs/storage/). – rmalviya May 12 '17 at 05:21
  • @rmalviya thanks, upvoted :-) – eko May 12 '17 at 05:22

1 Answers1

2

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);
}
rmalviya
  • 1,847
  • 12
  • 39
  • Thank you. What has been tripping me up is the (value) part of .then(). I understand when I use it with an http get. But I am not sure what is does in this example. – Thomas Degroot May 12 '17 at 14:10
  • `http.get()` returns an `observable` not `promise`. Refer to [this](http://stackoverflow.com/questions/37364973/angular-promise-vs-observable) question to understand difference. – rmalviya May 13 '17 at 12:42