I am trying to asynchronize my original code
for(var item in items) { dbAccess.Save(item); } which works fine:
var tasks = new List<Task>(); for(var item in items) { tasks.Add(dbAccess.SaveAsync(item)); } await Task.WhenAll(tasks); However, I need to add an additional clean-up call before I save and item to my DB:
var tasks = new List<Task>(); for(var item in items) { tasks.Add(dbAccess.DeleteAsync(item.id)); tasks.Add(dbAccess.SaveAsync(item)); } await Task.WhenAll(tasks); This code above is incorrect since SaveAsync should not be executed until the corresponding DeleteAsync is finished. In other words Deletion must always go before Saving for each item but the order for the items does not matter.
Is there a perfect way to do this ?