How to make functions async?

Hi everyone,

I have an expensive function that takes 3-4 seconds to complete. When I call this function inside of a script it, app freezes for 3-4 seconds till the result back. I have searched and seen async/await/task method. It says it is like javascript async/await but I coudn’t make it work. Like in javascript can I make my expensive function async so that it works in the background without freezing my unity app? How can I make function async and wait result in coroutine yield return or await?

    public void ExpensiveFunction()
    {
       // Lots of operations.
    }

     IEnumerator MyCoroutine()
    {
            await MyResult = ExpensiveFunction();
    }

You can start a pooled thread execution using BeginInvoke(). Then, wait for the result in a coroutine

// this is a coroutine body
{
     // concurrently run something
     System.Func<string> concurrentMethod = ExpensiveThreadedFunction;
     var concurrentResult = concurrentMethod.BeginInvoke(null, null);
     while (!concurrentResult.IsCompleted)
     {
              yield return Waits.waitForEndOfFrame;
     }
     string expensiveResult = concurrentMethod.EndInvoke(concurrentResult);
     Debug.Log(expensiveResult );
}

string ExpensiveThreadedFunction()
{
    // do something that takes very long 
}

IEnumerator ExpensiveFunction()
{
operation1();
yield return new WaitForSeconds(0.4f);
operation2();
}