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 =;
  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;
    if(result == true){

  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.
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 = % 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.


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.


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