0

I have an async function. There are multiple levels of function calls that all return a Task. At some point in this chain I must modify the value of the result. I would like to do this without interrupting the flow of the async functions. Observe:

[HttpGet]
[Route("GetWeb")]
public async Task<IHttpActionResult> GetResult([FromUri] string url)
{
    var result = await GetPageText(url);
    return Ok(result);
}

private Task<string> GetPageText(string url)
{
    Task<string> data = GetDataAsync(url);
    //here I would like to manipulate the data variable
    return data;
}

In the function GetPageText how do I manipulate the data variable without interrupting the asynchronous flow. Is this possible?

i3arnon
  • 113,022
  • 33
  • 324
  • 344
Luke101
  • 63,072
  • 85
  • 231
  • 359

1 Answers1

5

You can't change a task's value, you need to create a new one. The async way of doing it is awaiting the original result and marking the method as async (that's only the case when you need the result of the original task):

private async Task<string> GetPageText(string url)
{
    var result = await GetDataAsync(url);
    return result + "bar";
}
i3arnon
  • 113,022
  • 33
  • 324
  • 344