How to get the value from a Callback Function for AWS Cognito Sign Up

Viewed 148

I apologize if there is a very straightforward answer to this, but I am having trouble figuring out how to get the value for a callback function being passed as an argument to a pre-defined function.

Here is my current code:

function createNewUser() {
     (userPool.signUp(email, password, attributeList, null, callbackFunction,{"key":"test"}))
     
   //Get Value returned by Callback
}

function callbackFunction(err, result){
    console.log("in callback")
    console.log(err)
    console.log(result)
    return err
}

And here is the definition for the method parameters regarding a AWS Cognito User Pool Sign up function:

signUp(username: string, password: string, userAttributes: cognito.CognitoUserAttribute[], validationData: cognito.CognitoUserAttribute[], callback: cognito.NodeCallback<Error, cognito.ISignUpResult>, clientMetadata?: { [key: string]: string; }): void

All I want to do is to receive the value of either result or err from the callback function and reference it in the CreateNewUserFunction. I am not exactly sure how to do this/or if its even possible to change the parameters for callbackFunction even though it is defined as

callback: cognito.NodeCallback<Error, cognito.ISignUpResult>

Any advice would be much appreciated.

1 Answers

Here's a solution for your problem, you can do something like this:

async function createNewUser() {
     const result = new Promise((resolve, reject) => {

userPool.signUp(email, password, attributeList, null, (error, data) => {
  if(error) reject(error)
  else resolve(data)

},{"key":"test"})})
     
   //Get Value returned by Callback
}
Related