-1

Explanation of code

I have a function which extracts the value from an html input and returns a formatted address. I call this function twice storing each address into appropriate variable. Once I have converted both inputs I want to call another function, function z.

ngAfterViewInit() {
    //get input elements, get the first tag within the ion-input tag
    this.searches.startSearch = document.getElementById("startSearch").getElementsByTagName('input')[0];
    this.searches.endSearch = document.getElementById("endSearch").getElementsByTagName('input')[0];


    var address1 = this.placeDecode(this.searches.startSearch)
    var address2 = this.placeDecode(this.searches.endSearch)


    this.methodZ(address1,address2);

}

 //method convertes input into formatted address
  private placeDecode(input : HTMLInputElement) {

    var location = input.value;

    var geoCode = new google.maps.Geocoder();

    geoCode.geocode({
      address: location
    }, function (result, status) {
      if (status === 'OK') {
        return result[0].formatted_address;

      } else {
        return null;
      }
    });
  }

Problem

The problem I am having is I only want to call function Z once both input have been converted. I have tried using callback but i can't call a function z within a callback.

Community
  • 1
  • 1
Yusof Bandar
  • 169
  • 1
  • 4
  • 9
  • This function doesn't do anything. You're `return`ing into nothing. Why can't you call function `z` within a callback? Read and understand https://stackoverflow.com/q/14220321/476, then you probably want to use `Promise.all()` to await two promises. – deceze Jan 23 '18 at 14:49
  • I didnt show all my code because there is no need, you can imagine me calling placeDecode and retrieving results, when calling z within callback, it says it is undefined – Yusof Bandar Jan 23 '18 at 14:53
  • I can't really imagine how you call `placeDecode` *and retrieve the result*, because `placeDecode` doesn't return any result. – deceze Jan 23 '18 at 14:55
  • returns result[0].formatted_address – Yusof Bandar Jan 23 '18 at 14:59
  • Yeah, no. That's returning from an asynchronous callback, not from `placeDecode`. You really *really* need to read and understand this: https://stackoverflow.com/a/14220323/476 – deceze Jan 23 '18 at 15:01
  • Yep I can see, cheers man – Yusof Bandar Jan 23 '18 at 15:10

1 Answers1

0

I didn't really understand your explanation, but I think I understood your issue : you want to wait for two functions to end, and call a thrid one, right ?

This is an asynchronous issue, and the best way to resolve that is using Promises.

In your method called two times, change your return statement to this :

return Promise.resolve(/* your old return value here */);

Now, you can call it twice :

const callOne = myAsyncFunction(/* params */);
const callTwo = myAsyncFunction(/* params */);

Finally, you can wait for both to resolve (and avoid a callback hell) :

Promise.all([callOne, callTwo]).then(function(values) {
  console.log(values); // will contain an array of two items
});