Skip to content Skip to sidebar Skip to footer

How To Return Response From Cognito Async Function And Display It On The Page?

Given the code below, how will I output the response back on the webpage when the response is either success or fail? loginUser(data) { var authenticationData = { Usernam

Solution 1:

You can use an async function and Promise in order to get the result from the async call.

function loginUser(data) {
    var authenticationData = {};
    var authenticationDetails = new AmazonCognitoIdentity.AuthenticationDetails(authenticationData);
    var userPool = new AmazonCognitoIdentity.CognitoUserPool(config.cognito);
    var userData = {};
    var cognitoUser = new AmazonCognitoIdentity.CognitoUser(userData);
    var output = null;
    return new Promise(function(resolve, reject) {
      cognitoUser.authenticateUser(authenticationDetails, {
        onSuccess: resolve,
        onFailure: reject,
      });
    });    
}

async function main() {
    try {
      var output = await loginUser(data);
    } catch(e) {
      console.log(e);
    }
}

Post a Comment for "How To Return Response From Cognito Async Function And Display It On The Page?"