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

152
Vistas
Wait until function from external library is executed in TypeScript

I'm new to TypeScript (and JavaScript) and I'm having some issues to wait until a function from an external library, namely amazon-cognito-identity-js is executed. I want to first authenticate an user and then get a token from that user. That's it. I read this nice tutorial about Promises and there is also an example on the official page of the library, but seems like I'm missing something.

I created the following code:

import {
  AuthenticationDetails,
  CognitoUser,
  CognitoUserPool
} from "amazon-cognito-identity-js";

var DEV_USER_POOL_DATA = {
  UserPoolId: 'userPool',
  ClientId: 'clientId'
};

var USER_POOL_DATA = DEV_USER_POOL_DATA;

function getJWT() {
  return new Promise<void>((resolve) => { 
    resolve();
  });
}

function login() {
  let userPool = new CognitoUserPool(USER_POOL_DATA);
  let authenticationDetails = new AuthenticationDetails({
    Username: "myUser",
    Password: "myPass",
  });
  let cognitoUser = new CognitoUser({ Username: "myUser", Pool: userPool });

  cognitoUser.authenticateUser(authenticationDetails, {
    onSuccess: function(result) {
      return result.getAccessToken().getJwtToken();
    },
    onFailure: function(err) {
      console.log(err); 
    }
  });
}

getJWT().then(login);
console.log('Hello world');

However when I run the code I see

Hello world
<The actual token I will like to see first>

Do you know what is wrong?

about 4 years ago · Juan Pablo Isaza
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