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

135
Views
Helper Function not returning response but shows result with the function

I have this helper function:

const listFileKeysInSpaceByPrefix = async (prefix) => {
    var params = {
        Bucket: process.env.S3_BUCKET_NAME,
        Prefix: prefix,
    };
    let fileKeys;
    await s3.listObjects(params, function (err, data) {
        if (err) {
            console.log(err, err.stack);
            next(err);
        }
        let keys = [];
        data["Contents"].forEach(function (obj) {
            keys.push(obj.Key);
        });
        console.log("key1", keys);
        fileKeys = keys;
    });
    return fileKeys;
};

And I called the helper function within a route like this:

const keyToBeDeleted = await listFileKeysInSpaceByPrefix(uploadSpaceFolder);
console.log("keys",keyToBeDeleted);

The Result on my console however looks thus:

keys undefined
key1 ['adamter/rr0.png', 'adamter/rt0.png']

I need to get the array in my response when I call the function but I seem not to find a way around this.

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

Try with callback function.

const listFileKeysInSpaceByPrefix = (prefix, callBackFn) => {
  var params = {
    Bucket: process.env.S3_BUCKET_NAME,
    Prefix: prefix,
  };
  s3.listObjects(params, function (err, data) {
    if (err) {
        console.log(err, err.stack);
        next(err);
    }
    let keys = [];
    data["Contents"].forEach(function (obj) {
        keys.push(obj.Key);
    });
    console.log("key1", keys);
    callBackFn(keys);
  });
};

function yourFunction() { 
  function callBackFn(keyToBeDeleted) { 
    // access keyToBeDeleted 
    // add your logic here 
  }

  listFileKeysInSpaceByPrefix(uploadSpaceFolder, callBackFn); 
}
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!