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

151
Views
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
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!