Skip to content

Commit

Permalink
Merge pull request #404 from tildeio/onerror-label
Browse files Browse the repository at this point in the history
onerror is now provided (via 2nd arg) the promises label
  • Loading branch information
stefanpenner committed Aug 28, 2015
2 parents 6e61187 + 53289b1 commit 7684756
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 5 deletions.
18 changes: 16 additions & 2 deletions README.md
Expand Up @@ -175,6 +175,20 @@ RSVP.on('error', function(reason) {
});
```

`RSVP` allows Promises to be labeled: `Promise.resovle(value, 'I AM A LABEL')`
If provided, this label is passed as the second argument to `RSVP.on('error')`

```javascript
RSVP.on('error', function(reason, label) {
if (label) {
console.error(label);
}

console.assert(false, reason);
});
```


**NOTE:** promises do allow for errors to be handled asynchronously, so
this callback may result in false positives.

Expand Down Expand Up @@ -262,7 +276,7 @@ one of the following formats:
## Deferred

> The `RSVP.Promise` constructor is generally a better, less error-prone choice
> than `RSVP.defer()`. Promises are recommended unless the specific
> than `RSVP.defer()`. Promises are recommended unless the specific
> properties of deferred are needed.
Sometimes one needs to create a deferred object, without immediately specifying
Expand Down Expand Up @@ -345,7 +359,7 @@ RSVP.on('chained', listener);
RSVP.on('fulfilled', listener);
RSVP.on('rejected', listener);
```

Events are only triggered when `RSVP.configure('instrument')` is true, although
listeners can be registered at any time.

Expand Down
4 changes: 2 additions & 2 deletions lib/rsvp/events.js
Expand Up @@ -190,15 +190,15 @@ export default {
@param {*} options optional value to be passed to any event handlers for
the given `eventName`
*/
'trigger': function(eventName, options) {
'trigger': function(eventName, options, label) {
var allCallbacks = callbacksFor(this), callbacks, callback;

if (callbacks = allCallbacks[eventName]) {
// Don't cache the callbacks.length since it may grow
for (var i=0; i<callbacks.length; i++) {
callback = callbacks[i];

callback(options);
callback(options, label);
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion lib/rsvp/promise.js
Expand Up @@ -176,7 +176,7 @@ Promise.prototype = {
var promise = this;
config.after(function() {
if (promise._onError) {
config['trigger']('error', reason);
config['trigger']('error', reason, promise._label);
}
});
},
Expand Down
17 changes: 17 additions & 0 deletions test/extension-test.js
Expand Up @@ -1440,6 +1440,23 @@ describe("RSVP extensions", function() {
});
});

it("onerror gets label if provided", function(done) {
var thrownError = new Error();

RSVP.configure('onerror', function(error, label) {
assert.equal(error, thrownError, "The thrown error is passed in");
assert.equal(label, 'i am label', "the label for the given promise is also provided");
done();
});

new RSVP.Promise(function(resolve, reject) {
reject(thrownError);
}, 'i am label').then(function() {
// doesn't get here
assert(false);
});
});

it("When provided, handled exceptions are not sent to it", function(done) {
var thrownError = new Error();

Expand Down

0 comments on commit 7684756

Please sign in to comment.