Is it fine to await for one Task but not for other?

I have a following service method,

public async Task<IList<ProductsImage>> InsertAsync(BaseProduct product, Dictionary<string, Stream> images)
{
    try
    {
        if (images.Count > 0)
        {
            await _imageService.SaveAllAsync(images);
        }
        return await _repository.InsertAsync(product);
    }
    catch // Delete the files if there is an error from db
    {
        if (images.Count > 0)
        {
            var paths = images.Select(i => i.Key).ToList();
            _imageService.DeleteAllAsync(paths);                        
        }
        throw;
    }
}

The problem is that I don't want to await at _imageService.DeleteAllAsync because it takes some time to finish, so that I can transfer my execution without waiting. Is there any danger of not using await in _imageService.DeleteAllAsync

Upvotes: 3

Views: 149

Answers (1)

Sergey
Sergey

Reputation: 3213

The right way is to send this message to a message queue so it can be processed asynchronously (without await) by an outside process. This way it will also guarantee that DeleteAllAsync will get called or retried if it fails.

Here is my shameless plug for the article I wrote on this subject.

Upvotes: 1

Related Questions