Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

onerror is now provided (via 2nd arg) the promises label #404

Merged
merged 1 commit into from
Aug 28, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 16 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,20 @@ RSVP.on('error', function(reason) {
});
```

`RSVP` allows Promises to be labeled: `Promise.resovle(value, 'I AM A LABEL')`
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

s/resovle/resolve

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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