2

In my loop I have to get the address of lat and long. I have this function using reverseGeolocation

_getLocationAddress = async location => {
    return new Promise((resolve, reject) => {
      try {
        const { status, data } = await srs.getReverseGeolocation(location);
        if (data) {
          resolve(data.results[0].formatted_address);
        }
      } catch (err) {
        console.log(err);
        reject(err);
      }
    });
  };

I also tried not wrapping into promise and not async it doesn't work it keeps returning a promise object. What I need from there is to return the data result into string. Here's my render

renderNewSR() {
    const { delivery_items } = this.state;
    return delivery_items.map((prop, key) => {
      const location = {
        latitude: parseFloat(prop.pickuplat),
        longitude: parseFloat(prop.pickuplong)
      };
      //console.log(location);
      const address = "";

      this._getLocationAddress(location)
        .then(res => {
          console.log(res);
        })
        .catch(err => {
          console.log(err);
        });
.....

What is alternative solution to this. I want the result from google api return as string and can be displayed in render.

1
  • I have posted a solution below. Let me know if that solves your query. Commented Sep 1, 2019 at 7:00

1 Answer 1

1

You are not declaring arrow function async inside new Promise but you are trying to use await inside that function.

Just add async (resolve, reject) to solve the issue.

Also you don't need to use async in async location as you are not awaiting any promise but returning promise in wrapper function.

_getLocationAddress = async location => {
    return new Promise(async (resolve, reject) => {
      try {
        const { status, data } = await srs.getReverseGeolocation(location);
        if (data) {
          resolve(data.results[0].formatted_address);
        }
      } catch (err) {
        console.log(err);
        reject(err);
      }
    });
  };

Hope that helps!!!

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.