Aleksander Chelpski
Aleksander Chelpski

Reputation: 771

A ValueTask can be awaited only once - what does it mean?

I want to use ValueTask. I found somewhere that "a ValueTask can be awaited only once ". I cannot await for the method GetMoney multiple times or what? If yes, how should I change my code?

public class CreditCardService
{
    private Dictionary<int, int> _cache = new Dictionary<int, int>()
    {
        { 1, 500 },
        { 2, 600 }
    };

    public async ValueTask<int> GetMoney(int userId)
    {
        if (_cache.ContainsKey(userId))
        {
            return _cache[userId];
        }

        //some asynchronous operation:
        Random rnd = new Random();
        var money = rnd.Next(100);

        _cache[userId] = money;
        return await Task.FromResult(money);
    }
}

var creditCardService = new CreditCardService();
Console.WriteLine(await creditCardService.GetMoney(1));
Console.WriteLine(await creditCardService.GetMoney(3));
Console.WriteLine(await creditCardService.GetMoney(3));

Upvotes: 1

Views: 182

Answers (2)

knittl
knittl

Reputation: 265717

You are awaiting each task exactly once:

var creditCardService = new CreditCardService();
Console.WriteLine(await creditCardService.GetMoney(1)); // a new ValueTask
Console.WriteLine(await creditCardService.GetMoney(3)); // a new ValueTask
Console.WriteLine(await creditCardService.GetMoney(3)); // a new ValueTask

The following would be awaiting a single task multiple times:

var creditCardService = new CreditCardService();
var money1Task = creditCardService.GetMoney(1)
Console.WriteLine(await money1Task);
Console.WriteLine(await money1Task);

Upvotes: 3

wohlstad
wohlstad

Reputation: 29009

Each time you call GetMoney you get a new ValueTask instance.

Then you await each of these ValueTask instances exactly once.

Therefore your code is not violating this rule.

In order to violate the rule, you would need to await the same ValueTask instance more than once.

Some more information about ValueTasks can be found in this .NET blog: Understanding the Whys, Whats, and Whens of ValueTask.

Upvotes: 4

Related Questions