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

243
Visualizações
Using a variable outside an arrow function in JS

I have set up a websocket that reads all e-mail addresses as a backend and sends them to the client. Now I want the used memory space to be sent along with the quota. The mysql-Query results in an array with several JSON strings (results):

{ id: 10,
    domain_id: 6,
    email: 'mailadress',
    password: 'password',
    quota: 5368709120,
    crypt: 2,
    wsplx: 'customer',
    quota_used: undefined }

As a further value I want to enter “quota_used.” That’s why I wrote the following code:

for (value of results) {
  console.log(get_used_quota(value.email))
  value.quota_used = get_used_quota(value.email);
}
socket.emit("response_get_update_mails", (results));

function get_used_quota(email) {
  var mailadress_arr = email.split("@");
  var cmd = "du -sb /var/vmail/" + mailadress_arr[1] + "/" + mailadress_arr[0] + "/Maildir/";
  exec(cmd, (error, stdout, stderr) => {
    if (error) {
      showtoaster("\"" + `${error.message}` + "\"", "Ein Fehler ist aufgetreten!", "warning", true, true, 3000)
      return;
    }
    if (stderr) {
      showtoaster("\"" + `${stderr}` + "\"", "Ein Fehler ist aufgetreten!", "warning", true, true, 3000)
      return;
    }
    var output = `${stdout}`;
    var size_arr = output.split("\t");
    size = parseInt(size_arr[0]);
    return size
  })
}

The problem is that I can’t get the value “size” out of the arrow function. The return value of the function remains undefined. Can someone help me to fix this problem, this would be very nice? Let my know, if you need more information.

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

0

You can wrap that call in a promise:

function get_used_quota(email) {
  var mailadress_arr = email.split("@");
  var cmd = "du -sb /var/vmail/" + mailadress_arr[1] + "/" + mailadress_arr[0] + "/Maildir/";
  return new Promise(resolve => {
    exec(cmd, (error, stdout, stderr) => {
      if (error) {
        showtoaster("\"" + `${error.message}` + "\"", "Ein Fehler ist aufgetreten!", "warning", true, true, 3000)
        return;
      }
      if (stderr) {
        showtoaster("\"" + `${stderr}` + "\"", "Ein Fehler ist aufgetreten!", "warning", true, true, 3000)
        return;
      }
      var output = `${stdout}`;
      var size_arr = output.split("\t");
      size = parseInt(size_arr[0]);
      resolve(size);
    });
  });
}

and await the result

value.quota_used = await get_used_quota(value.email);
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