Question: I am new to JavaScript and have been trying to get this working for a while now but to no success.

I have a function (task 3) that should only be executed after the functions before it finish. The functions before it (task 1 and 2) have more functions in them that fetch data from other sources and the time it takes for them is unknown. A wait function wouldn't really work because the time for task 1 and 2 could be very fast or very slow. I have tried doing an async/await setup but i must have done it wrong because it always completed task 3 before task 1 or 2. Same with a callback function which didn't actually callback it just did task 1 and 2 then never did task 3.

```
function task1(input){
   // has more functions that do other stuff

}
function task2(input){
   // has more functions that do other stuff
}
function task3(input){
   // this code should only be executed after both task 1 and 2 finish
}

function main(input1, input2, input3){
    task1(input1); // doesn't matter which task finishes first between 1 and 2
    task2(input2);
    task3(input3); // task 3 should not be executed until task 1 and 2 have completed.
}

main(input1, input2, input3);

```

If anyone could help that would be greatly appreciated.

Answer: 
By generating an object with keys which are the `Person` values from `result`, we can use that to efficiently filter the values in `invoices`, and then use map to return only the `id` values of the filtered result:

```js
const invoices =  [
    {id: 90, Client: 'Bob', paid: false, total: 900},
    {id: 91, Client: 'Sarah', paid: false, total: 400}
]

const result =  [{km: 200, hours: 20, Person: 'Sarah'}]

const resindexes = result.reduce((c, o, i) =>
  (c[o.Person] = i, c), {});

ids = invoices.filter(o => resindexes[o.Client] === undefined).map(o => o.id);

console.log(ids);
```
Or you can do it by make persons Map.

Plus of that attempt is you have access to person data by client name. 

*Notice I incremented **invoices** for 1 row for better result view.*

```js
// Data enter code here
const invoices = [
    {id: 90, Client: 'Bob', paid: false, total: 900},
    {id: 91, Client: 'Sarah', paid: false, total: 400},
    {id: 92, Client: 'Joe', paid: false, total: 400}
];
const personsArr = [{km: 200, hours: 20, Person: 'Sarah'}];

// Query
const persons = new Map(
    personsArr.map(r =>{ 
        return [r.Person, r]
    }
  )
);
// invoices which do not have results
const orphans = invoices.filter((i)=>!persons.has(i.Client));
// id of invoices which do not have results
const orphansId = orphans.map((o)=>o.id)

console.log(orphansId);
```

it returns: [
 90,
 92
]