H
Size: a a a
H
ΑZ
p
map : (a -> b) -> Promise a ->
(
match IsPromise b
case Promise x => Promise x
case NotPromise => Promise b
)
p
H
p
map : (a -> b) -> Promise a ->
(
match IsPromise b
case Promise x => Promise x
case NotPromise => Promise b
)
H
H
ΑZ
p
p
p
p
ΑZ
async Task<object> Map(Task<T> task, Func<T, TResult> func) {
var result = func(await task);
var resultType = typeof(TResult);
if (resultType.IsGenericType && resultType.GetGenericDefinition() == typeof(Task<>) {
return await GetAwaiter(result)
}
return result;
}
ΑZ
ΑZ
public static class JsHelper
{
public static async Task<object> Map<T>(this Task<T> task, Func<T, object> func)
{
var result = func(await task);
if (result is Task t)
{
await t;
var taskType = t.GetType();
if (taskType.IsGenericType)
{
return taskType.GetProperty(nameof(Task<object>.Result)).GetValue(t);
}
return null;
}
return result;
}
}
async Task Main()
{
(await Task.FromResult(10).Map(t => t * 2)).Dump();
(await Task.FromResult(10).Map(t => Task.Delay(2000).ContinueWith(_ => t * 2))).Dump();
}
p
ΑZ