Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

242
Views
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 answers
Answer question

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!