Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

353
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda