I was reading through Future Method Documentation and found that you can only return void type with this method, is there any particular reason that method returns void type ?
- Anybody can ask a question
- Anybody can answer
- The best answers are voted up and rise to the top
Future methods will run in the future. You don't want your synchronous code waiting an unknown period of time for an asynchronous bit of code to finish working.
The whole point is you want to clearly separate what happens now from what happens in the future.
By only returning void, you can't have code that waits for a result.
It's also why you can only pass Ids and primitives as parameters. It's assumed the future method will execute at an unknown time in the future, so if you need database records you'll need to re-query the data to ensure it's fresh.
(I can't add a comment under Nick as my rep is low, but...)
To add to Nick Cook's answer,
There is a relatively new interface in Salesforce (Oct '14?), Queueable, which works the same as @future. That is, the platform queues up a process when the line of code is executed, and runs it at a later time in a separate execution.
Some key differences, and reasons why you should know about Queueable as well as @future -
This can be useful for either of the following -
Here's more info about Queueables: https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_queueing_jobs.htm
Note: With a little Apex Magic, you could have a future handler class which delegates between @future and Queueable, and using this class you can always make a callouut or queue another future process. As far as I can see, this requires that you pass information as primitive data.
Note2: Queueable itself is not always able to make callouts, so to fully leverage, you'll need both Queueables and @futures.