Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

249
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda