Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

104
Visualizações
Return value from Geocode Promise

I'm using the react-geocode api to get coordinates from a string address (like 1234 home street, etc.). So far, this is my code.

async function ConvertAddress(address) {
      if (address !== "") {
        Geocode.fromAddress(address, resolve).then(
            (response) => {
              const { lat, lng } = response.results[0].geometry.location;
              const center = { lat: lat, lng: lng}; // if I console logged center from here, it would work fine.
              return center;


            },
            (error) => {
              alert("ConvertAddress error:", error);
            }
            
        );
      }
    }

However, I'm not sure how to access "center" from outside this function (such as from another function). I've tried this:

console.log("ConvertAddress test", ConvertAddress(data[0]));

And it returns this:

ConvertAddress test 
Promise { <state>: "fulfilled", <value>: undefined }
​
<state>: "fulfilled"
​
<value>: undefined
​
<prototype>: Promise.prototype { … }

Can I have some help with returning center in such a way that I can call it without it being undefined? Thanks in advance.

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

Try this

async function ConvertAddress(address) {
      if (address !== "") {
       var addressResult = await Geocode.fromAddress(address, resolve).then(
            (response) => {
              const { lat, lng } = response.results[0].geometry.location;
              const center = { lat: lat, lng: lng}; // if I console logged center from here, it would work fine.
              return center;
            },
            (error) => {
              return "error in results"
            }
        );
        addressResult.then(function(result){ 
            console.log(result);
        }) 
      }
    }
about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda