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

356
Views
How to wrap function call inside of Promise

After exporting the required modules and setting up the variables

let AWS = require("aws-sdk");
const AmazonCognitoIdentity = require('amazon-cognito-identity-js');

const USER_POOL_ID = 'us-east-1_vkXRQuP4U';
const CLIENT_ID = 'mipa4trls0l7323om33mlk80e8';

const poolData = { 
  UserPoolId : USER_POOL_ID, ClientId : CLIENT_ID
};
const POOL = new AmazonCognitoIdentity.CognitoUserPool(poolData);

let email = "my.email@domain.com";
let password = "My.Password!";

I can go ahead and call signUp command:

POOL.signUp(email, password, [], null, function(err, result) {
  console.log('...result:', result);
});

And it works well. Next I want to wrap the POOL.signUp(email, password...) inside async function sign_up like so:

async function sign_up(email, password) {
  POOL.signUp(email, password, [], null, function(err, result) {
    console.log('...sign_up.result:', result);
    return result;
  })
};

async function main() {
  let signupData = await sign_up(email, password);
  console.log('...main.signupData:', signupData);
  return signupData;
};

main().then((error, data) => {console.log('...error, data:', error, data)});

While it works fine, the order of the calls that get executed is wrong as the main function doesn't wait for the sign_up() function to complete. In attempt to correct this behavior I wrap the POOL.signUp(email, password...) inside of Promise:

async function sign_up(email, password) {

  return await new Promise((resolve) => {
    POOL.signUp(email, password, [], null, {
      onSuccess: (result) => {
        console.log('...result:', result)
        return resolve(result);
      },      
      onFailure: (err) => {
        return resolve(err.message);
      },
    });
  })
};

But I am getting the error message:

 UnhandledPromiseRejectionWarning: TypeError: callback is not a function

Is there a way to avoid this error?

over 4 years ago · Santiago Trujillo
1 answers
Answer question

0

  • don't need to await the Promise you are returning (since we precisely want our function to be async, we want to defer the waiting to the caller)
  • Promise constructor function needs to provide the second reject parameter to be able to access the callback in the function implementation
  • pass a callback function as your POOL.signUp fourth argument, instead of an object
function sign_up(email, password) {
  return new Promise((resolve, reject) => {
    POOL.signUp(email, password, [], null, function(err, result) {
      if (err) {
        return reject(err.message);
      }

      console.log('...result:', result)
      resolve(result);
    });
  })
};
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!