[英]Control not being returned from promise
我正在尝试使用 AWS Cognito 注册用户。 在下面的class
,没有返回registerInCognito
的控件。 我也尝试过在失败和成功条件之后添加返回(拒绝和解决承诺是否相同?)。 但没有任何效果。 当我尝试调试时,我发现我评论的部分<---- The control is unreachable here.
没有被执行。 控件本身在返回函数中。
export class CompanyCAO { // change return type from any to meaningful type private poolData; private pool_region; private userPool; private cognitoUser; constructor() { this.poolData = { UserPoolId: process.env.COGNITO_USER_POOL_ID, ClientId: process.env.COGNITO_CLIENT_ID } this.pool_region = process.env.COGNITO_POOL_REGION; this.userPool = new AmazonCognitoIdentity.CognitoUserPool(this.poolData); } public create(regInfo: Company): Promise < any > { return new Promise < boolean > (async(resolve, reject) => { this.registerInCognito(regInfo); }); } public registerInCognito(regInfo: Company): Promise < boolean > { return new Promise < boolean > (async(resolve, reject) => { let attributes = process.env.COGNITO_AUTH_ATTRIBUTES!.split(","); let attributeValues = Array < string > (); let attributesList = Array < any > (); for (var attribute in attributes) { for (var attributeName in regInfo) { if (((typeof regInfo[attributeName]) != 'object') && (attributes[attribute] == attributeName)) { attributeValues.push(regInfo[attributeName]); } else if ((typeof regInfo[attributeName]) === 'object') { for (var subAttributeName in regInfo[attributeName]) { if (subAttributeName == attributes[attribute]) { attributeValues.push(regInfo[attributeName][subAttributeName]); } } } } } for (var index in attributes) { attributesList.push(new AmazonCognitoIdentity.CognitoUserAttribute({ Name: attributes[index], Value: attributeValues[index] })); } const randomPassword = this.createRandomPassword(); console.log(`Password is ${randomPassword}`); await this.userPool.signUp(regInfo.Admin.EmailAddress, randomPassword, attributesList, null, (err, result) => { if (err) { console.error(err); reject(err); } else { this.cognitoUser = result.user; } }).promise().then((value: any) => { resolve(true); }).catch(error => { reject(error); }) }); // <---- The control is unreachable here. } public createRandomPassword() { let password = ''; const alphaCharacters = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'; const numberCharacters = '1234567890'; const specialCharacters = '!@#$%^&**()?/><}{'; const iterations = 5; for (let i = 0; i < iterations; i++) { password += alphaCharacters.charAt(Math.floor(Math.random() * alphaCharacters.length)); password += numberCharacters.charAt(Math.floor(Math.random() * numberCharacters.length)); password += specialCharacters.charAt(Math.floor(Math.random() * specialCharacters.length)); } return password; } }
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.