question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

MapResult Usage with async methods

See original GitHub issue

I’m using it CommandLineParser from and async Task Main method.

I would like to use verbs, so I would use the recommended syntax:

CommandLine.Parser.Default.ParseArguments<Options1, Options2, Options3>(args)
	.MapResult(
	  (Options1opts) => Method1(opts),
	  (Options2opts) => Method2(opts),
	  (Options3opts) => Method2(opts),
	  errs => 1);

But in my case, the methods are async. Howo do I use the MapResult in this scenario? The same problem occurs with the WithParsed methods.

Thanks!

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Reactions:6
  • Comments:9 (4 by maintainers)

github_iconTop GitHub Comments

15reactions
nemeccommented, Apr 28, 2018

@shravan2x I’m not sure I understand. OP’s methods return tasks already so there’s no issue. Here is a full example of what I’m talking about.

async Task Main()
{
	var args = "a --name tim".Split();
	await CommandLine.Parser.Default.ParseArguments<Options1, Options2, Options3>(args)
		.MapResult(
	  (Options1 opts) => Method1(opts),
	  (Options2 opts) => Method2(opts),
	  (Options3 opts) => Method3(opts),
	  errs => Task.FromResult(0));
}

public async Task<int> Method1(Options1 opt)
{
	Console.WriteLine("Method1 " + opt.Name);
	await Task.Delay(TimeSpan.FromSeconds(1));
	return 1;
}

public async Task<int> Method2(Options2 opt)
{
	Console.WriteLine("Method2");
	await Task.Delay(TimeSpan.FromSeconds(1));
	return 2;
}

public async Task<int> Method3(Options3 opt)
{
	Console.WriteLine("Method3");
	await Task.Delay(TimeSpan.FromSeconds(1));
	return 3;
}

[Verb("a")]
public class Options1
{
	[Option('n', "name")]
	public string Name { get; set; }
}

[Verb("b")]
public class Options2
{
}

[Verb("c")]
public class Options3 
{
}
0reactions
SuperJMNcommented, May 4, 2018

Thanks for the info! Now I know how to proceed 😃

Read more comments on GitHub >

github_iconTop Results From Across the Web

C# - .MapResultAsync Not Working + DisplayHelp not ...
I have found a fix to the problem. internal static async Task CLCLPE(string[] args) { var parser = new Parser(p => { p....
Read more >
Multiprocessing Pool Get Result from Asynchronous Tasks
There are two ways that we can get results from asynchronous tasks executed with the multiprocessing pool. They are: Use a result callback....
Read more >
Parallelization in Python - Kanishk Varshney - Medium
You will have to fetch the result from the MapResult object. Fetching the results from the Map Async takes a similar time as...
Read more >
How to use async functions with Array.map in Javascript
First, it needs to map every item to a Promise with the new value, which is what adding async before the function does....
Read more >
A Guide to Asynchronous Array Iterator Functions
First, using the the asynchronous map method as mentioned before, we calculate whether the element passes the filter function test and resolve ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found