Question: I am struggling with waiting till some things are done in the function that is being called inside a **for loop**.

I have several Divs stored in an array and the thing I want to achieve is:

1. One randomly chosen div highlights for 1s
2. After the previously chosen div stops highlighting some other randomly chosen div starts to do the same thing

So I want to make them highlight in a random sequence.

I have been trying different things from the internet, however can't make it to work the way it should. Usually the loop just go through all the divs and there is no waiting or anything. They are all highlighted at the same time.

The code below has promise with which I was trying to achieve the final goal, but I am not pretty sure if it is the correct approach. I have also tried using **await/async**, but probably I don't know how to use them properly, so it didn't work.

```
function startHiglightingBlocks()
    {
        sequenceDivs = [];
        var randomIndexes = shuffle(indexes);
        clickIndex = 0;
        for(var i=0; i < sequenceLength; ++i)
        {
            sequenceDivs.push(blockDivs[randomIndexes[i]]);
            highlightBlock(blockDivs.eq(randomIndexes[i]))
                    .then(
                        function (result) {

                        }
                    );
        }
    }

    function highlightBlock(block)
    {
        block.addClass('highlight');
        return new Promise(function (resolve, reject) {
            setTimeout(function () {
                block.removeClass('highlight');
                block.text(i)
                resolve("Success");
            }, 1000);
        });
    }

```

Answer: 
@Ketan Ramteke is right. Or you can return a list and use that value like this:

```
void main(){
    int num1 = 10;
    int num2 = 11;

    print("First : $num1, $num2");

    List arr = swap(num1,num2);

    print("Last : ${arr[0]}, ${arr[1]}");
}

List swap(int num1,int num2){
    int temp;

    temp = num1;
    num1 = num2;
    num2 = temp;
    return [num1, num2];
}

```
```

// Dart 2.6.
void main(){
    int num1 = 10;
    int num2 = 11;

    print("First : $num1, $num2");

    // get returned swapped values
    List result = swap(num1,num2);
    // assign them to original variables
    num1 = result[0];
    num2 = result[1];
    //print the mutated num1 and num2
    print("Last : $num1, $num2");

}
List swap(int num1,int num2){
    int temp;

    temp = num1;
    num1 = num2;
    num2 = temp;
   // print("Last : $num1, $num2");
    return [num1, num2];

}

```