0

Intro:

So I am new to JavaScript and I would like to know how I can use the return from an async function without using .then or anything of that sort (if not, then is it possible to use this in a way that I can access the return in scope of the signupData() function). So I am encrypting things with ECIES scheme using Secp256k1 keys to be sent to the API for later usage, however, I think the .encrypt under the encryptMes section in the post invokes a promise. Below is relevant code:

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 function:

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: How can I access the return of the async function, encryptMes, without ".then", so I can use it in the scope of the signupData() function in the html file?

  • 1
    "*use the return from an async function without using .then or anything of that sort (if it's possible)*" - no, it's not possible. Just mark `signupData` as `async` as well so that you can `await` the `encryptMes` call – Bergi Jan 24 '21 at 09:24
  • 1
    Btw you will probably also want to await the `apiPost()` request before reloading the page. And make sure to prevent the default action of the form submit event. – Bergi Jan 24 '21 at 09:26

1 Answers1

0

if your signupData func was async too then you could use the await keyword variable = await asyncfunct(params)

Torge Rosendahl
  • 482
  • 6
  • 17