Shopify/batman — pull request #329 by jamesmacaulay (2012-02-14 00:18:10+00:00)
Title: Promises

A convenient shorthand for a common batman.js pattern: you want to define an accessor that lazy-loads some resource asynchronously, calling `@set` on itself when the value is known.

Example usage:

```coffeescript
class AsyncThing extends Batman.Object
@accessor 'eventually', promise: (deliver, key) -> MyModel.find(1, deliver)
```

The given "fetcher" function gets called with two arguments: `deliver` and `key`. `deliver` is a function which expects to be called with the standard `(error, result)` arguments, so it works out-of-the-box with Batman model finders, for example (as above). `key` is the same one passed to `get`, so it's only really useful you're defining a default accessor, or one for multiple keys.

The implementation uses the base class or object's existing default accessor to take care of the actual storage, so if you use this on a `Batman.Object` then it'll store the value as a vanilla JS property, and if you use it on a `Batman.Hash` then it'll put the value in its hash storage.

If the `deliver` function gets called _synchronously_, then the getter just returns the value without using the setter.