The result will hopefully already be available when needed, and the async call will be made only once.
fooFuture = fetchFoo !_
# anytime when needed:
foo = fooFuture _ # supports multiple calls!The async call will again be made only once, but it won't happen until it's needed.
barFuture = null
# anytime when needed:
barFuture or= fetchBar !_
bar = barFuture _ # like pattern 1 above
The first pattern could be problematic if you have several tasks competing for the resource the first time it is accessed. The problem is that several
fetchFoocalls will be launched in parallel (foo or= fetchFoo _is not atomic). I typically use afunnelto avoid that but your second pattern gives an interesting alternative to solve it:Note: the
fooFuture or= fetchFoo !_instruction is atomic. It does not yield so there is no risk of launchingfetchFoomore than once.