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

129
Visualizações
map function concludes after one successful execution

I am trying to map an array and fetch relevant data from MySQL and then again map the array returned by MySQL and send tweets. With this logic, the app only sends tweet for one element in the array. But, it actually needs to send multiple tweets as it maps through the array. Here is the code:

array.items.map(item => {
  con.query(`SELECT username FROM users where country="${item.country}"`, async function (err, result, fields) {
    if (err) throw err;
    console.log(result);

    result.length > 0 && result.map(async user => {
      try {
        await sendTweet(`Hello ${user.username}, your selected country is ${item.country}`);
      } catch (error) {
        console.error(error.body)
        return false
      }
    })
  });
});

What am I doing wrong here? I'd appreciate any help. Thank you.

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

0

It seems there's no need to async and await.

var array = [1, 2, 3];
array.map(item => {

  // con.query
  setTimeout(function(err, result, fields) {
    if (err) throw err;

    result = [11, 22, 33];

    result.length > 0 && result.map(user => {
      try {

        // sendTweet
        setTimeout(function() {
          console.log(`Hello ${user.username}, your selected country is ${item.country}`);
        }, 500);

      } catch (error) {
        console.error(error)
        return false
      }
    })
  }, 500);
});

about 4 years ago · Juan Pablo Isaza Relatório

0

I think you can use the function Promise.all

if (result.length) {
  try {
      await Promise.all(result.map(user => {
          return sendTweet(`Hello ${user.username}, your selected country is ${item.country}`);
      }));
  } catch(e) {
      //Handle the error
  }
}
about 4 years ago · Juan Pablo Isaza Relatório

0

This is how I would maybe write it

async function tweetAll(item, error, users) {
  if(error) {
    console.log('error', error.message)
  }
  else {
    for(user of users) {
      await sendTweet(`Hello ${user.username}, your selected country is ${item.country}`)
    }
  }
}

for(item of array.items){
  con.query(`SELECT username FROM users where country="${item.country}"`, (error, users) => tweetAll(item, error, users))
}
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