How to make Update() wait for coroutine to finish?

I have a method ProcessTurn() that is called in Update(). If enemyTurn = true, then ProcessTurn() starts the coroutine OnEnemyAction().

OnEnemyAction() loops through each enemy in the array enemyParty, then prints the name of that enemy to console. Then it waits 2 seconds before printing the next name.

The problem is: I only want enemyTurn = false when the coroutine has finished printing the very LAST name in the array. But instead it makes enemyTurn = false as soon as the coroutine is started.

Where do I put enemyTurn = false?

void ProcessTurn()
{
    if(enemyTurn)
    {
        StartCoroutine(OnEnemyAction());
        enemyTurn = false;
    }
}

IEnumerator OnEnemyAction()
{
    foreach(GameObject enemy in enemyParty)
    {
        Debug.Log(enemy.name);

        yield return new WaitForSeconds(2);
    }
}

void ProcessTurn()
{
if(enemyTurn)
{
StartCoroutine(OnEnemyAction());
}
}
IEnumerator OnEnemyAction()
{
foreach(GameObject enemy in enemyParty)
{
Debug.Log(enemy.name);
yield return new WaitForSeconds(2);
}
enemyTurn = false;
}

The coroutine is started and your code continues running from the next line immediately.
Set value to false in the coroutine iteself after your other stuff is done.

From @ATate’s answer: To make Update() wait for a coroutine to finish, you need a boolean. If bool = true: StartCoroutine(). Then set the bool = false in IEnumerator().

Also, if you have a foreach loop inside the coroutine, but only want the foreach loop to run once, you need another boolean to check whether the coroutine has already started. As shown in ATate’s reply.

Special thanks to ATate for helping me to figure this out!