Search Unity

  1. We are migrating the Unity Forums to Unity Discussions. On July 12, the Unity Forums will become read-only. On July 15, Unity Discussions will become read-only until July 18, when the new design and the migrated forum contents will go live. Read our full announcement for more information and let us know if you have any questions.

Alternative for async/await in WebGL?

Discussion in 'Web' started by Tarodev, Apr 18, 2019.

  1. Tarodev

    Tarodev

    Joined:
    Jul 30, 2015
    Posts:
    190
    I'd much prefer to not go through my entire project to replace all of my async/awaits with coroutines... Is there something I can replace await with which works in webgl?
     
    Ceperadlo likes this.
  2. Katori

    Katori

    Joined:
    Nov 30, 2009
    Posts:
    60
    Why do you think that async/await doesn't work on WebGL? I've used async/await on WebGL just fine.
     
  3. Tarodev

    Tarodev

    Joined:
    Jul 30, 2015
    Posts:
    190
    How exactly?
    A simple await Task.Delay() causes a threading error.

    For example, a project with just this one script will fail:

    Code (CSharp):
    1. public class AwaitTest : MonoBehaviour
    2. {
    3.     public GameObject Ball;
    4.     void Start() {
    5.         Drop();
    6.     }
    7.  
    8.     private async void Drop() {
    9.         while (true)
    10.         {
    11.             Instantiate(Ball, Vector3.zero, Quaternion.identity);
    12.             await Task.Delay(500);
    13.         }
    14.     }
    15. }
     
    Last edited: Apr 18, 2019
  4. Katori

    Katori

    Joined:
    Nov 30, 2009
    Posts:
    60
    That is not (just) async. That is System.Threading. You cannot use System.Threading on WebGL (yet).
     
  5. Tarodev

    Tarodev

    Joined:
    Jul 30, 2015
    Posts:
    190
    You're right. So only alternative are coroutines?
     
  6. aqeel25

    aqeel25

    Joined:
    Apr 8, 2019
    Posts:
    25
    Have you got solution , I am also facing this problem
     
  7. GustavNinja

    GustavNinja

    Joined:
    Jun 13, 2016
    Posts:
    101
    UniTask is a good plugin.
     
    nguyenlamlll and NFTcraft like this.
  8. NitinAltran

    NitinAltran

    Joined:
    Mar 9, 2021
    Posts:
    8
    Hi I am currently using async await for API calls along with Unity Web Request should I avoid it if I am planning for deploying on WebGL?
     
  9. aqeel25

    aqeel25

    Joined:
    Apr 8, 2019
    Posts:
    25
    Yes You should avoid it. Because at the end it will not work in WebGl . I have faced similar issue. Use UniTask Instead.
     
    nguyenlamlll likes this.
  10. EtienneCowley

    EtienneCowley

    Joined:
    Jul 12, 2013
    Posts:
    2
    Hi Katori, how did you go about doing that because it looks like you can't?
     
  11. ferretnt

    ferretnt

    Joined:
    Apr 10, 2012
    Posts:
    417
    Aync/Await absolutely works with WebGL, it's System.Threading that doesn't. Also, the new 2023.1 "Awaitables" (so await Awaitable<T> rather than Task<T>) absolutely work with WebGL, and will let you.

    await Awaitable.WaitForSecondsAsync() works in WebGL, and will let you do the same as a task.delay.

    https://docs.unity3d.com/2023.1/Documentation/ScriptReference/Awaitable.WaitForSecondsAsync.html

    Bonus points for the fact that using Awaitable<T> means that async/await actually works sanely in the editor.

    Don't await BackgroundThreadAsync() though, that just drops off a cliff, since Awaitable doesn't change the fact that threads don't exist in WebGL.
     
    Last edited: Feb 20, 2024
  12. adamgolden

    adamgolden

    Joined:
    Jun 17, 2019
    Posts:
    1,558
    I can also confirm async/await works in my projects' WebGL builds without issue (first started using in 2022 LTS and am still using in 2023.3 alphas).