# Need explanations for async function

**URL:** <https://forum.dfinity.org/t/need-explanations-for-async-function/16863>\
**Category:** Language Support\
**Created:** [November 25, 2022, 9:53am UTC](https://forum.dfinity.org/t/need-explanations-for-async-function/16863 "2022-11-25T09:53:54Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![passio](https://sea1.discourse-cdn.com/flex023/user_avatar/forum.dfinity.org/passio/32/8091_2.png) [@passio](https://forum.dfinity.org/u/passio)\
**Post date:** [November 25, 2022, 9:53am UTC](https://forum.dfinity.org/t/need-explanations-for-async-function/16863/1 "2022-11-25T09:53:54Z")

</div>

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

```auto
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 :

```auto
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 :

```auto
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.

 ![Capture d’écran 2022-11-25 à 10.53.06](https://us1.discourse-cdn.com/flex023/uploads/dfn/original/2X/c/c108899b70fc58d10c8f40964a33cc0058880ed9.png)

---

<div class="post-metadata">

**Author:** ![Vivienne](https://sea1.discourse-cdn.com/flex023/user_avatar/forum.dfinity.org/vivienne/32/29170_2.png) [@Vivienne](https://forum.dfinity.org/u/Vivienne)\
**Post date:** [November 25, 2022, 12:25pm UTC](https://forum.dfinity.org/t/need-explanations-for-async-function/16863/2 "2022-11-25T12:25:54Z")

</div>

> [@passio](#):
>
> ` var a = generateNumber()`

this should be `var a = await generateNumber()`

---

<div class="post-metadata">

**Author:** ![passio](https://sea1.discourse-cdn.com/flex023/user_avatar/forum.dfinity.org/passio/32/8091_2.png) [@passio](https://forum.dfinity.org/u/passio)\
**Post date:** [November 26, 2022, 11:24am UTC](https://forum.dfinity.org/t/need-explanations-for-async-function/16863/3 "2022-11-26T11:24:46Z")

</div>

Thank you Severin. I think I tried this before and I though it wasn’t working… But, I will try again.

So, I tried :

 ![Capture d’écran 2022-11-26 à 12.25.59](https://us1.discourse-cdn.com/flex023/uploads/dfn/original/2X/5/5b34bd2afa2a3c23781f0335ce040efede0a83f4.png)  
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…

---

<div class="post-metadata">

**Author:** ![Icdev2dev](https://avatars.discourse-cdn.com/v4/letter/i/c2a13f/32.png) [@Icdev2dev](https://forum.dfinity.org/u/Icdev2dev)\
**Post date:** [November 27, 2022, 8:28am UTC](https://forum.dfinity.org/t/need-explanations-for-async-function/16863/4 "2022-11-27T08:28:13Z")

</div>

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](https://github.com/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.

---

<div class="post-metadata">

**Author:** ![passio](https://sea1.discourse-cdn.com/flex023/user_avatar/forum.dfinity.org/passio/32/8091_2.png) [@passio](https://forum.dfinity.org/u/passio)\
**Post date:** [November 27, 2022, 8:39am UTC](https://forum.dfinity.org/t/need-explanations-for-async-function/16863/5 "2022-11-27T08:39:39Z")

</div>

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