Intro
因此,我是JavaScript新手,我想知道如何使用异步函数的返回,而无需使用.then或其他任何类型的返回(如果没有,是否可以使用这种方式访问signupData()函数的返回范围)。所以我用ECIES方案加密东西,使用Secp256k1密钥发送到API供以后使用,但是,我认为post中encryptMes部分下的.encrypt调用了一个承诺。以下是相关守则:
signup.html
<form id="login-form" name ="signup-form">
<input class="login-form-field" type="text" name="user" placeholder="username">
<input class="login-form-field" type="text" name="email" placeholder="email">
<input class="login-form-field" type="password" name="dob" placeholder="date of birth">
<br>
<!--<button class="actionButton"></button>-->
<INPUT TYPE="button" class="button-success" NAME="button" Value="sign up" onClick="signupData(this.form)">
<br>
<div class="signup">
<a href="login.html">login</a>
</div>
</form> function signupData(form) //add to this script
{
console.log("signup data is starting");
var user = form.user.value;
var email = form.email.value;
var dob = form.dob.value;
console.log("checkpoint: 1");
genSKey();
console.log("checkpoint: 2");
console.log("checkpoint: 3");
var enUser = encryptMes(user); //why does this invoke a promise?
var enDOB = encryptMes(dob);
var data = {name:"LifeNet", members:{}} //you added members to the same area in the object so it is always replacing members since it's the field of data
data.members[enUser] = {profilePic:{},enDOB, listeners:{}, listening:{}, friends:{}, requested:{}, blocked:{}, channel:false}
console.log("checkpoint: 3");
console.log({data});
apiPost({data});
//pass the signup function in here
//hash the variables and send to celox network
//console.log(JSON.stringify({data}));
//alert (`copy and save your Private Key to somewhere safe: ${skey}`);
//window.location.href= "login.html";
}encryptMes函数:
window.encryptMes = async function(data)
{
//for this you need to get the sender's public key to encrypt the message
console.log("encryptmes: began");
var pkey = genPKey();
if (pkey === null || undefined)
{
console.log('You do not have a key pair');
}
var encryptedMes = await eccrypto.encrypt(pkey, Buffer.from(data));
var enMes = encryptedMes.toString('hex');
console.log(encryptedMes); //could be this since it is not stringified when it goes into celox network
console.log(enMes);
return enMes;
}Focus:如何在不使用".then“的情况下访问异步函数encryptMes的返回,以便在html文件中的signupData()函数的作用域中使用它?
发布于 2021-01-24 09:25:22
如果您的signupData函数也是异步的,则可以使用等待关键字variable = await asyncfunct(params)。
https://stackoverflow.com/questions/65868891
复制相似问题