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

120
Visualizações
Returing positions using axios

I am trying to iterate through an array of objects and call my API using the value of the object. And then log in to the console. My API is returning the values needed, but the output is this: [ Promise { <pending> }, Promise { <pending> } ]

My function:

async function geoEncode() {
  const clients = [
    {
      nome: "John",
      address: "Jardim Planalto Sinibaldo Cassino 33",
    },

    {
      nome: "Joseph",
      address: "R. Afrizia Martins Sanacato - Jardim Natalia",
    },
  ];
  const baseUrl = "https://api.tomtom.com";
  const endPoint = "search/2/geocode";
  const format = ".json";
  const apiKey = "Hq2eTTQvtJRna5MMAsAsARlsQCxv5XjR";
  const clientsPositions = clients.map(async (client) => {
    const address = client.address;
    const position = await axios
      .get(`${baseUrl}/${endPoint}/${address}${format}/?key=${apiKey}&limit=1`)
      .then((response) => response.data.results[0].position)
      .catch((error) => error.detailedMessage);
    return position;
  });
  return clientsPositions;
}

geoEncode().then((result) => console.log(result));
about 4 years ago · Juan Pablo Isaza
2 Respostas
Responde à pergunta

0

You are issuing multiple calls to the API and thus creating multiple promises. What you are seeing in the console is an array of those promise objects. You need to write code like below to wait for all of them to complete.

...
Promise.all(geoEncode()).then((values) => {
  console.log(values);
});
...
about 4 years ago · Juan Pablo Isaza Relatório

0

async function geoEncode() {
  const clients = [
    {
      nome: "John",
      address: "Jardim Planalto Sinibaldo Cassino 33",
    },

    {
      nome: "Joseph",
      address: "R. Afrizia Martins Sanacato - Jardim Natalia",
    },
  ];
  const baseUrl = "https://api.tomtom.com";
  const endPoint = "search/2/geocode";
  const format = ".json";
  const apiKey = "Hq2eTTQvtJRna5MMAsAsARlsQCxv5XjR";
  const clientsPositions = Promise.all(
    clients.map(async (client) => {
      const address = client.address;
      const position = await axios
        .get(
          `${baseUrl}/${endPoint}/${address}${format}/?key=${apiKey}&limit=1`
        )
        .then((response) => response.data.results[0].position)
        .catch((error) => error.detailedMessage);
      return position;
    })
  );
  return clientsPositions;
}

geoEncode().then((result) => console.log(result));

Promise.All () ensures that the array of all sync codes is executed successfully, and returns a single promise that resolves to an array of the results.

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