getsentry/raven-js — pull request #144 by matthewwithanm (2013-10-17 21:07:36+00:00)
Title: Return result of wrapped function from context

This adds one word ("`return`") and a test (:

The idea is that it makes the context method more of a transparent wrapping. This comes in handy with anything where the return value matters—for example, RequireJS:

**Without Raven**

```javascript
define(function (require) {
var result = doSomething();
return {
something: result.anything
};
});
```

**With Raven**

```javascript
define(function (require) {
var result;
// require raven yada yada

Raven.context(function () {
result = doSomething();
});

return {
something: result.anything
};
});
```

**With Raven + This Change**

```javascript
define(function (require) {
// require raven yada yada

return Raven.context(function () {
var result = doSomething();
return {
something: result.anything
};
});
});
```

This allows you wrap your entire module with little change (and without having to worry if you missed some errors that were done after the context call). It's even nicer in CoffeeScript thanks to the implicit returns:

**Without Raven**

```coffeescript
define (require) ->
result = doSomething()

# Exports
something: result.anything
```

**With Raven + This Change**

```coffeescript
define (require) ->
# require raven yada yada

Raven.context ->
result = doSomething()

# Exports
something: result.anything
```

Basically, you just need to indent your code an extra level, as opposed with the current behavior where you need to create a variable in the outer context and close over it.

These are small examples to get the point across but obviously it has more of an impact as the amount of code grows.