Soy nuevo en TypeScript (y JavaScript) y tengo algunos problemas para esperar hasta que se ejecute una función de una biblioteca externa, a saber, amazon-cognito-identity-js . Primero quiero autenticar a un usuario y luego obtener un token de ese usuario. Eso es todo. Leí este buen tutorial sobre Promises y también hay un ejemplo en la página oficial de la biblioteca , pero parece que me estoy perdiendo algo.
Creé el siguiente código:
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');Sin embargo, cuando ejecuto el código veo
Hello world <The actual token I will like to see first>¿Sabes lo que está mal?