Skip to content
This repository was archived by the owner on Jan 12, 2024. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ static string GetTenantUriFromHeader(System.Net.Http.Headers.AuthenticationHeade
/// <returns>A <see cref="Task"/> encapsulating the access token.</returns>
public async Task<string> GetAccessTokenAsync(CancellationToken cancellationToken)
{
var application = applicationLazy.Value;
var application = await applicationLazy.Value;

try
{
Expand Down
38 changes: 14 additions & 24 deletions src/Azure/Azure.Quantum.Client/Utility/LazyAsync.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,36 +9,26 @@

namespace Microsoft.Azure.Quantum.Utility
{
internal sealed class LazyAsync<T>
internal sealed class LazyAsync<T> : Lazy<Task<T>>
{
private readonly Lazy<Task<T>> instance;
private readonly Lazy<T> valueL;

/// <summary>
/// Constructor for use with synchronous factories
/// Initializes a new instance of the <see cref="LazyAsync{T}"/> class.
/// Constructor for use with asynchronous factories.
/// </summary>
public LazyAsync(Func<T> synchronousFactory)
: this(new Lazy<Task<T>>(() => Task.Run(synchronousFactory)))
{
}
/// <param name="taskFactory">Async value factory.</param>
public LazyAsync(Func<Task<T>> taskFactory)
: base(() => Task.Run(taskFactory))
{ }

/// <summary>
/// Constructor for use with asynchronous factories
/// Initializes a new instance of the <see cref="LazyAsync{T}"/> class.
/// Constructor for use with synchronous factories.
/// </summary>
public LazyAsync(Func<Task<T>> asynchronousFactory)
: this(new Lazy<Task<T>>(() => asynchronousFactory()))
{
}

// private constructor which sets both fields
private LazyAsync(Lazy<Task<T>> instance)
{
this.instance = instance;
this.valueL = new Lazy<T>(() => this.instance.Value.GetAwaiter().GetResult());
}

public T Value => valueL.Value;
/// <param name="valueFactory">Sync value factory.</param>
public LazyAsync(Func<T> valueFactory)
: base(() => Task.Run(valueFactory))
{ }

public TaskAwaiter<T> GetAwaiter() => instance.Value.GetAwaiter();
public TaskAwaiter<T> GetAwaiter() => Value.GetAwaiter();
}
}