# Async\* await\* Does this do what I think it does?

**URL:** <https://forum.dfinity.org/t/async-await-does-this-do-what-i-think-it-does/17227>\
**Category:** Language Support\
**Tags:** Motoko\
**Created:** [December 8, 2022, 9:57pm UTC](https://forum.dfinity.org/t/async-await-does-this-do-what-i-think-it-does/17227 "2022-12-08T21:57:56Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![skilesare](https://sea1.discourse-cdn.com/flex023/user_avatar/forum.dfinity.org/skilesare/32/5609_2.png) [@skilesare](https://forum.dfinity.org/u/skilesare)\
**Post date:** [December 8, 2022, 9:57pm UTC](https://forum.dfinity.org/t/async-await-does-this-do-what-i-think-it-does/17227/1 "2022-12-08T21:57:56Z")

</div>

This is what I use locally to call things that I don’t want to actually wait on a round?

```auto
private func _x(y : Nat) : async* Nat{
     y*2
};

public shared(msg) func x(y: Nat) : async Nat{
     await* _x(y);
};

```

Can async\* functions themselves have awaits inside of them?

```auto
private func _x(y : Nat) : async* Nat {
     switch(Map.get<Nat, Nat>(my_cache, y){
           case(null){
                let remote_result = await other_actor.calc(y);
                Map.set<Nat,Nat>(my_cache, y, remote_result);
           };
           case(?val){val};
     };
};

public shared(msg) func x(y: Nat) : async Nat{
     await* _x(y);
};

```

Does await now “Always” wait a round of consensus?

> **[Release 0.7.4 · dfinity/motoko](https://github.com/dfinity/motoko/releases/tag/0.7.4)**
>
> motoko (moc)
> 
> 
> Add new keywords async\* and await\* (note the \*) for efficient abstraction of asynchronous code (#3609).
> ::= ...
> async\* delayed, asynchronous computation...

---

<div class="post-metadata">

**Author:** ![skilesare](https://sea1.discourse-cdn.com/flex023/user_avatar/forum.dfinity.org/skilesare/32/5609_2.png) [@skilesare](https://forum.dfinity.org/u/skilesare)\
**Post date:** [December 8, 2022, 11:19pm UTC](https://forum.dfinity.org/t/async-await-does-this-do-what-i-think-it-does/17227/2 "2022-12-08T23:19:47Z")

</div>

When will 0.7.4 make it into dfx for use?

---

<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:** [December 9, 2022, 7:17am UTC](https://forum.dfinity.org/t/async-await-does-this-do-what-i-think-it-does/17227/3 "2022-12-09T07:17:39Z")

</div>

0.7.4 will be in the next release, a beta version is planned for today or early next week.

---

<div class="post-metadata">

**Author:** ![skilesare](https://sea1.discourse-cdn.com/flex023/user_avatar/forum.dfinity.org/skilesare/32/5609_2.png) [@skilesare](https://forum.dfinity.org/u/skilesare)\
**Post date:** [January 23, 2023, 4:48pm UTC](https://forum.dfinity.org/t/async-await-does-this-do-what-i-think-it-does/17227/4 "2023-01-23T16:48:36Z")

</div>

> [@Vivienne](#):
>
> 0.7.4

If I want dfx build to use the latest (0.7.6) motoko, what do I need to do? I’m also using vessel:

```auto
"defaults": {
    "build": {
      "args": "",
      "packtool": "vessel sources"
    },
    "replica": {
      "subnet_type": "system"
    }
  },

```

Where do I need to put vessel? Do I need to download moc as well? Where do I put that?

---

<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:** [January 24, 2023, 7:58am UTC](https://forum.dfinity.org/t/async-await-does-this-do-what-i-think-it-does/17227/5 "2023-01-24T07:58:24Z")

</div>

Here’s how to use a different Motoko version: [DFX deploy with custom Motoko version](https://forum.dfinity.org/t/dfx-deploy-with-custom-motoko-version/7665)

> [@skilesare](#):
>
> Where do I need to put vessel?

Anywhere in your `$PATH`.

> [@skilesare](#):
>
> Do I need to download moc as well?

`vessel` does it for you.

---

<div class="post-metadata">

**Author:** ![skilesare](https://sea1.discourse-cdn.com/flex023/user_avatar/forum.dfinity.org/skilesare/32/5609_2.png) [@skilesare](https://forum.dfinity.org/u/skilesare)\
**Post date:** [January 24, 2023, 2:27pm UTC](https://forum.dfinity.org/t/async-await-does-this-do-what-i-think-it-does/17227/6 "2023-01-24T14:27:29Z")

</div>

I ended up having to put moc into the cache/dfx0.12.1/ folder and give it execute.

Maybe I should be calling something other than dfx build? Or maybe I should have installed a beta version of dfx and I would have gotten this for free?

---

<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:** [January 24, 2023, 2:48pm UTC](https://forum.dfinity.org/t/async-await-does-this-do-what-i-think-it-does/17227/7 "2023-01-24T14:48:27Z")

</div>

I thought of replacing moc in the cache folder but thought it’s a bit too hacky to recommend. It works quite well, you just have to think of doing it again if you ever change dfx versions.

If you build using a canister of type `motoko`, then setting the env var is probably the cleanest. You can also export that one in your `.bashrc` or some other dotfile if you want it globally.

> [@skilesare](#):
>
> Or maybe I should have installed a beta version of dfx and I would have gotten this for free?

No, it’s not in a beta version yet. The dfx beta that just released still only contains 0.7.4.

---

<div class="post-metadata">

**Author:** ![rabbithole](https://sea1.discourse-cdn.com/flex023/user_avatar/forum.dfinity.org/rabbithole/32/8138_2.png) [@rabbithole](https://forum.dfinity.org/u/rabbithole)\
**Post date:** [January 24, 2023, 5:53pm UTC](https://forum.dfinity.org/t/async-await-does-this-do-what-i-think-it-does/17227/8 "2023-01-24T17:53:09Z")

</div>

> [@skilesare](#):
>
> I ended up having to put moc into the cache/dfx0.12.1/ folder and give it execute.
> 
> Maybe I should be calling something other than dfx build? Or maybe I should have installed a beta version of dfx and I would have gotten this for free?

Set compiler version to “0.7.6” in `vessel.dhall` file and download the compiler with  
`vessel verify --version 0.7.6` and then pass it to dfx:  
`DFX_MOC_PATH="$(vessel bin)/moc" dfx build <canister_name>`

---

<div class="post-metadata">

**Author:** ![icaten](https://sea1.discourse-cdn.com/flex023/user_avatar/forum.dfinity.org/icaten/32/9897_2.png) [@icaten](https://forum.dfinity.org/u/icaten)\
**Post date:** [January 25, 2023, 5:33pm UTC](https://forum.dfinity.org/t/async-await-does-this-do-what-i-think-it-does/17227/9 "2023-01-25T17:33:22Z")

</div>

Is there an example of when specifically using an async\* declaration could cause conflicts in concurrent access to state between callers, as opposed to a standard async declaration (that wouldn’t)?

Very exciting feature to have as part of Motoko.

---

<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:** [January 26, 2023, 7:27am UTC](https://forum.dfinity.org/t/async-await-does-this-do-what-i-think-it-does/17227/10 "2023-01-26T07:27:26Z")

</div>

I don’t think async\* changes access to state semantics, but I’ll ask the Motoko team as well.

---

<div class="post-metadata">

**Author:** ![claudio](https://sea1.discourse-cdn.com/flex023/user_avatar/forum.dfinity.org/claudio/32/322_2.png) [@claudio](https://forum.dfinity.org/u/claudio)\
**Post date:** [January 26, 2023, 10:44am UTC](https://forum.dfinity.org/t/async-await-does-this-do-what-i-think-it-does/17227/11 "2023-01-26T10:44:39Z")

</div>

An `await*`-ed `async*` expression will only yield control to the scheduler/allow interleaving if its body directly (or any `async*` that it `await*s`) does a proper, old fashioned `await`.

So think of the `*` as this may do 0 or more `await`s. If 0, there is not possibility of concurrent interaction. If 1 or more, you need to be careful. If you don’t know how many, you need to be conservative and careful and avoid leaving any shared state (visible by other concurrent calls) in an inconsistent state.
