hacklang

Running async task in the background in Hacklang


I'm wondering if there is a way to run some async task in the background while other processing is happening in Hacklang, and then once the other processing is done, check if the result from the other async task is complete, and use the response if it is? There are some time sensitive things that need to be done, that can't be blocking.

public async function doStuff() {
   $background_res = doSomethingAsync();

   // do a bunch of important processing

   // get result of $background_res if it completed, if not move on
}

Solution

  • In Hack, you can await multiple tasks at once:

    concurrent{
      $background_awaitable = await doSomethingAsync();
      $important_result = await importantProcessing();
    }
    

    However, this will wait until all are done before continuing. If you want to not wait until both are done, you will need to look into a separate threading library