Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to make an Asynchronous Method return a value?

I know how to make Async methods but say I have a method that does a lot of work then returns a boolean value?

How do I return the boolean value on the callback?

Clarification:

public bool Foo(){     Thread.Sleep(100000); // Do work     return true; } 

I want to be able to make this asynchronous.

like image 535
Overload119 Avatar asked May 18 '11 13:05

Overload119


People also ask

How do you make an asynchronous function return a value?

To return values from async functions using async-await from function with JavaScript, we return the resolve value of the promise. const getData = async () => { return await axios.

Can async return value?

Async functions always return a promise. If the return value of an async function is not explicitly a promise, it will be implicitly wrapped in a promise.

What are the possible return types of asynchronous method?

Async methods have three possible return types: Task<TResult>, Task, and void. In Visual Basic, the void return type is written as a Sub procedure. For more information about async methods, see Asynchronous Programming with Async and Await (Visual Basic).


1 Answers

From C# 5.0, you can specify the method as

public async Task<bool> doAsyncOperation() {     // do work     return true; }  bool result = await doAsyncOperation(); 
like image 115
Dave Arkell Avatar answered Sep 29 '22 17:09

Dave Arkell