Thursday, November 22, 2018

How to handle a timeout synchronously when sending a request to an API?

Hi, I am not too familiar with how cancellation tokens work but most of the solutions relating to timeouts mentions using them. I am using Auth0 and noticed that the delete user API call does not return anything. _client.Users.DeleteAsync only returns an empty task so I cannot use .Result to wait for the result. I need to call _client.Users.GetAsync after the delete request has been successful (did not timeout) so I need to synchronously wait for it to finish (as I do not want to use the await/async keywords).

Will the code below execute the if-block if a timeout occurs? If not, how do I do this correctly?

Thank you.

var token = new CancellationToken(); _client.Users.DeleteAsync(auth0UserId).Wait(3000, token); if (token.IsCancellationRequested) { // does this get executed if there is a timeout // (request took longer than 3 seconds)? } 

Also, if an API call returned an object, how do I wait with a timeout and also receive the object result? Wait() seems to only return a boolean.

How to handle a timeout synchronously when sending a request to an API? Click here
  • Blogger Comment
  • Facebook Comment

0 comments:

Post a Comment

The webdev Team