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

79
Views
Lambda is not waiting for Async/Await

Lamda ends the process and it's not waiting for the API response. here is my code:

exports.handler = async (event, context, cb) => {

 try {
    console.log('FETCHING RECORDS FROM DATOCMS')
    const allItems = await client.items.all({}, { allPages: true })
    if (!fs.existsSync(backupDir)){
        // Create backup directory
        console.log('CRETING BACKUP DIRECTORY')
        fs.mkdirSync(backupDir, { recursive: true });
    }

    console.log('FILL JSON WITH RECORDS FETCHED FROM DATOCMS')
    fs.writeFileSync('backup/records.json', JSON.stringify(allItems, null, 2));

    console.log('ADDING BACKUP FOLDER TO ZIP')
    zip.addLocalFolder(backupDir);
    const zipFileContents = zip.toBuffer();
    
    const params = {
        Bucket: S3_BUCKET,
        Key: `backup_zip-${backupDate}`,
        Body: zipFileContents,
        ContentType: "application/zip",
        ContentDisposition: `attachment; filename="${fileName}-${backupDate}"`
    }

    console.log('UPLOADING ZIP TO S3', params)
    s3.putObject(params, (err, data) => {
        if (err) {
            cb(null, 'Error')
            throw new Error('The zip file could not be uploaded', err);
        }
        console.log('Uploaded correctly')
        cb(null, 'Success')
        fs.rmdirSync(backupDir, { recursive: true });
    })
 } catch (err) {
    cb(null, 'Error..')
    throw new Error('An error occurred', err)
 }
}

I also tried adding this two lines with no luck

 setInterval(() => {}, 1000);
 context.callbackWaitsForEmptyEventLoop = false;

Can anyone help me? I've been testing Promises, Async/Await, Callbacks and no luck with that. Thanks!

This is the response in CloudWatch

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

0

Try to map the promises, then await on Promise.all. I'm not sure if all returns array of promises.

const promises =  client.items.map(it=>...)
const promises =  client.items.all(it=>...)
await Promise.all(promises)
about 4 years ago · Juan Pablo Isaza Report

0

You aren't waiting for s3.putObject. It should be:

const data = s3.putObject(params).promise();

You handle the error in a try/catch.

about 4 years ago · Juan Pablo Isaza Report

0

Remove the async keyword from the handler. You can't use the callback function in the async/await function.

Try to use the aws sdk function with .promise();

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!