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

289
Views
return Promise when invoking Lambda function

I'm trying to invoke a Lambda function and return a Promise on finish,

but i get the following error:

"createUser(...).then is not a function"

const createUser = (phone) => {
  return lambda.invoke({
    FunctionName: 'createUser',
    Payload: JSON.stringify({"phone": phone})
  }, (err, data) => {
    let payload = JSON.parse(data.Payload);

    if (payload.statusCode != 200) {
      Promise.reject(payload);
    }
    Promise.resolve(payload);
  })
}

createUser('')
  .then(res => console.log(res))
  .catch(err => console.log(err))

Already tried declaring a new Promise using the

let promise = new Promise((resolve, reject) => {...})

but that didn't work also...

over 4 years ago · Santiago Trujillo
2 answers
Answer question

0

aws-sdk supports promises through a promise() property on the object returned from most API methods, like so:

const createUser = (phone) => {
    return lambda.invoke({
        FunctionName: 'createUser',
        Payload: JSON.stringify({"phone": phone})
    }).promise();
}

createUser('555...')
.then(res => console.log)
.catch(err => console.log);

https://docs.aws.amazon.com/sdk-for-javascript/v2/developer-guide/using-promises.html

over 4 years ago · Santiago Trujillo Report

0

Alright,figured it out...

I haven't returned the promise correctly.

to fix the error, I have declared the lambda.invoke inside of a new Promise, and then returned the Promise like this:

const createUser = (phone) => {
   let promise = new Promise((resolve, reject) => {
    lambda.invoke({
      FunctionName: 'createUser',
      Payload: JSON.stringify({"phone": phone})
    }, (err, data) => {
      let payload = JSON.parse(data.Payload);
      if (payload.statusCode != 200) {
        reject(payload);
      } else {
        resolve(payload);
      }
    })
  })
  return promise;
}
over 4 years ago · Santiago Trujillo 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!