[SalesForce] How to synchronize asynchronous Apex

There are different ways to offload long-running processes to asynchronous apex: we got future calls, scheduled Apex, batch Apex and, more recently, Queueable Apex.

They are all good to launch "fire and forget" methods: they all have a return type of void.

Is there any other asynchronous mechanism where we can offload some work to an asynchronous method, then wait until it finishes to continue processing? I would like to avoid callbacks, but rather sleep one thread until the asynchronous processing has finished.

Or is that something that has been deliberately disabled by Salesforce to avoid running out of threads?

Best Answer

Salesforce/apex does not support threads. There is no way to synchronize asynchronous apex. But if you want as a workaround, you can update some backend field in some object from the asynchronous apex and write a trigger on the object. Then using trigger you can continue remaining execution. It is not recommended.

Note - If you are okay to wait till asynchronous process is complete, use synchronous code instead of asynchronous(if you are not hitting governor limits).