• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Devaka Cooray
  • Liutauras Vilda
  • Jeanne Boyarsky
  • Bear Bibeault
Sheriffs:
  • Paul Clapham
  • Knute Snortum
  • Rob Spoor
Saloon Keepers:
  • Tim Moores
  • Ron McLeod
  • Piet Souris
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Tim Holloway
  • Frits Walraven
  • Ganesh Patekar

What difference does the async keyword make in the azure function syntax?

 
Ranch Hand
Posts: 1131
4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

The azure function syntax has optional async keyword and the function can return a value. If this keyword is removed what difference will it make?





thanks.

 
Saloon Keeper
Posts: 10434
223
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This has nothing to do with Azure. It's a keyword of the C# programming language.

The async keyword makes it so that the thread that executes the method can temporarily 'exit' the method when it encounters the await keyword in the body of the method, so it can resume doing other useful stuff while the task inside the method that is being awaited is blocking execution.

An async method must declare that it returns either a Task or a void. The nice thing about async is that you don't actually have to return a Task instance with the return keyword, the language will do it for you. Here is an example:

WeatherService represents an external service that may take a relatively long time to return a response. Instead of making the thread that calls the GetWeatherReportAsync() method sit around and do nothing, the await keyword allows it to exit the method and go and do something else. When a response returns from the external service, the virtual machine will try to grab the original thread that was executing the method if it is idle, and let it execute the remainder of the async method.

You can write the same logic without async, but you'll see that it is much less pleasant:
 
Monica Shiralkar
Ranch Hand
Posts: 1131
4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks. If this is a http trigger call then does it have to wait in such cases for the slow operation to get over to get a return value?


So this is keyword of C# programming language and has nothing to do with azure functions. azure functions can be written in Java too. Since there is no such keyword in Java, how is the same thing achieved in java?
 
Stephan van Hulst
Saloon Keeper
Posts: 10434
223
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In Java the preferred way to do this is to submit a task to an ExecutorService. However, in many frameworks creating your own executors is usually a bad idea. I don't know if Azure provides executors for you to submit your tasks to. I think your best bet is to ask this question on an Azure message board.
 
Monica Shiralkar
Ranch Hand
Posts: 1131
4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!