Need explanations for async function

i everyone, I am stuck with this problem for 2 days now.
I have a function :

public func secure_eval_game_2(nb_choosed:Text, amount:Float) : async Text{
  var c = Time.now()/1000;
  var a = c % 6+1;
  var nb_rand = Int.toText(a);
  var result:Bool = nb_rand == nb_choosed;
  if(amount > wallet or amount < 0.01 or amount > 1){
    return 0;
  }
  else{
    if(result == true){
      credit_wallet(mise*(6-1));
    }
    else{
      debit_wallet(mise);
    }
  };

  return nb_rand;
};

In this easy function we :
ā†’ generate number between 1 and 6 (nb_rand)
ā†’ We compare the chosen number (by user) with the (pseudo) random number.
ā†’ We modify the wallet balance according to result Win/lose.
ā†’ At the end, we return the random_number, this random number is given in the .js file, and then, we can use it and display it in browser.

My question is :
Does it exist any way to return nb_rand in the JS and execute credit_wallet/debit_wallet (modify balance wallet) after it has been returned ? (To improve experience, because the Js render is blocked, waiting for execution in blockchain).

The only way i found is : to do this in my js :

nb_rand = motokoFile.generateNumber(); // No writing process in blockchain, only read, so we instantly 
// get the random_number to display it.
displayInJs(nb_rand);
motokoFile.evaluation_result(nb_rand); //unsecure

However, in this way, i guess that nb_rand could be intercepted in JS and a different number could be give to the motokoFile.evaluation_result(nb_rand) function.

I am completely completely completely stuck with async and none-async function in motoko fileā€¦ And this is probably why I canā€™t do it.

Other example :

public query func generateNumber(): async Text{
  var c = Time.now()/1000 % 100 ;
   return c;
};
public function test() : async Int{
  var a = generateNumber()
  var b = a + 1;   // problem
};

I get this kind of error and I have no idea about what I am supposed to do to solve it. (This error is not for this particular case). I guess that itā€™s because a is returned y an async func and b must wait for a to be evaluated, but I donā€™t know which keyword or synthax use.

this should be var a = await generateNumber()

1 Like

Thank you Severin. I think I tried this before and I though it wasnā€™t workingā€¦ But, I will try again.

So, I tried :


I donā€™t understand why it doesnā€™t work.

Do you know why ? Iā€™m little beginner in programming but I try my bestā€¦ However, I often spend one day to solve only 1 errorā€¦ sometime 2 daysā€¦ But this one, I didnā€™t find any solution. I canā€™t re-use my function inside an other function and have to copy/past the code in each of themā€¦

So this is a interesting pattern if you think about the state transitions. I have written up the actual working prototype hereā€¦GitHub - icdev2dev/icstatetransition1

Essentially the way I look at it, the game has three states.

WG(WaitingForGuess),
WRC(WaitingForRandomChoice),
Done

The key insight is that after the user inputs in a guess in the WG(WaitingForGuess) state (the transitions that a new game lands up in), the system moves to the WRC(WaitingForRandomChoice). In this state, there is nothing for the User to do. The system should automatically generate the random guess in WRC(WaitingForRandomChoice) and then move to the Done state. This is all done using heartbeats. So there is no need to invoke a function async/sync because of the poor UX

Many thanks to @GLdev for bringing up the StateTransition as a way of thinking about playing games.

3 Likes

Thank you for your answer, I will try to understand this.