Not enough material around using TryAsync and all the operators in general #920
Replies: 4 comments 2 replies
-
Use There's plenty of documentation here Fundamentally though, public Try<int> Foo() =>
() => 100; Is just a delegate:
public TryAsync<int> Foo() => async
() => await Task.FromResult(100); They both essentially represent a lazily evaluated union between |
Beta Was this translation helpful? Give feedback.
-
Thanks for the reply. Of course i dint even look at the wiki :O sorry. I have been using try Async like below: await EnsureBrowserIsAvailable()
.Match(
browserRevision => browserRevision,
e =>
{
AnsiConsole.MarkupLine("[red]ERROR FETCHING BROWSER[/]");
AnsiConsole.WriteException(e);
return Option<RevisionInfo>.None;
}
)
.BindAsync(browserRevision => EnsureBrowserIsLaunched()
.Match(
browser => browser,
e =>
{
AnsiConsole.MarkupLine("[red]ERROR LAUNCHING BROWSER[/]");
AnsiConsole.WriteException(e);
return Option<Browser>.None;
}
).ToAsync()
)
.IfSomeAsync(async browser =>
{
await TryAsync(browser.NewPageAsync())
.Match(page => page, e =>
{
AnsiConsole.MarkupLine("[red]ERROR OPENING NEW PAGE[/]");
AnsiConsole.WriteException(e);
return Option<Page>.None;
})
.ToAsync()
.IfSomeAsync(async page =>
{
... DO WORK IF PIPE SUCESSFUL.....
}); Where EnsureBrowserIsAvailable is defined as: private static TryAsync<RevisionInfo> EnsureBrowserIsAvailable()
{
var browserFetcher = new BrowserFetcher();
return TryAsync(browserFetcher
.DownloadAsync());
} I want to switch to Aff, Could i possibly get some pointers? I'm still struggling with understanding sorry, I'm hoping if you can see my current understanding you can tell me where I'm going wrong? Thanks for any help. |
Beta Was this translation helpful? Give feedback.
-
Lots of usage of I don't have access to all the types here, so I'll create some stub code so you can see a possible alternative approach. First up, the logging you're doing isn't ideal, it's injecting side-effects without really capturing the common pattern. We'd prefer to put it into an static AffCatch<A> CatchError<A>(string msg) =>
@catch(e => true, LogError<A>(msg));
static Func<Exception, Aff<A>> LogError<A>(string msg) =>
exception =>
{
AnsiConsole.MarkupLine(msg);
AnsiConsole.WriteException(e);
return FailAff<A>(exception);
};
Next, I'll create a couple of dummy types to represent the Browser and Page: public record Browser
{
public Task<Page> NewPageAsync() =>
Task.FromResult(new Page());
}
public record Page
{
} Next, I'll wrap up the static Aff<int> EnsureBrowserIsAvailable =>
SuccessAff(0) | CatchError<int>("[red]ERROR FETCHING BROWSER[/]");
static Aff<Browser> EnsureBrowserIsLaunched =>
SuccessAff(new Browser()) | CatchError<Browser>("[red]ERROR LAUNCHING BROWSER[/]");
static Aff<Page> NewPage(Browser browser) =>
browser.NewPageAsync().ToAff() | CatchError<Page>("[red]ERROR OPENING NEW PAGE[/]"); Note how I use the Now, I can replace your whole expression with something much more declarative and easier to use: from revision in EnsureBrowserIsAvailable
from browser in EnsureBrowserIsLaunched
from page in NewPage(browser)
from work in DoWorkWithPage(page)
select work;
static Aff<Unit> DoWorkWithPage(Page page) =>
SuccessEff(unit); You can invoke the Finally, this looks to me like a UI automation project? If so, you might be interested in a project that my team have worked on that wraps Selenium in a less brittle functional interface - we use it for our automation testing. |
Beta Was this translation helpful? Give feedback.
-
Hey I have another question :) I have this code I feel could be improved,...
I would really like to do somthing like this:
But accountsToLoop has left the Aff realm, not sure how I can put it back in and make a sequence.. if that makes sense? LoopAccountsAndBattle looks like this:
and Battle this:
As you can see i end up calling run twice...and one from inside the other. I know this is bad which is why I think there must be a better way. |
Beta Was this translation helpful? Give feedback.
-
Hi I'm struggling finding any good reading material on using this library.... I cant find anywhere that describes best ways to use TryAsync for example?
All the examples do not seem to be real world examples.. like making http calls, handling the results etc.
There are not many examples on chaining these operators together, to create functional chain with success and failure paths?
Thanks
Beta Was this translation helpful? Give feedback.
All reactions