Commit 2e98c2d0 authored by Alex Navasardyan's avatar Alex Navasardyan

Adding RSVP#rethrow

Rethrow makes sure that any exception in your promise chain won't
swallowed:

getJSON('/posts.json')
    .then(handlePost)
    .fail(RSVP.rethrow)

You can also to chain it further, like so:

getJSON('/posts.json')
    .then(handlePost)
    .fail(RSVP.rethrow)
    .then(null, handleRejection)
parent 16f95c05
...@@ -11,6 +11,7 @@ It works in node and the browser. You can get the browser build in ...@@ -11,6 +11,7 @@ It works in node and the browser. You can get the browser build in
## downloads ## downloads
- [rsvp-latest](http://rsvpjs-builds.s3.amazonaws.com/rsvp-latest.js) - [rsvp-latest](http://rsvpjs-builds.s3.amazonaws.com/rsvp-latest.js)
- [rsvp-latest (amd)](http://rsvpjs-builds.s3.amazonaws.com/rsvp-latest.amd.js)
## Promises ## Promises
......
...@@ -3,6 +3,7 @@ import { Promise } from "./rsvp/promise"; ...@@ -3,6 +3,7 @@ import { Promise } from "./rsvp/promise";
import { denodeify } from "./rsvp/node"; import { denodeify } from "./rsvp/node";
import { all } from "./rsvp/all"; import { all } from "./rsvp/all";
import { hash } from "./rsvp/hash"; import { hash } from "./rsvp/hash";
import { rethrow } from "./rsvp/rethrow";
import { defer } from "./rsvp/defer"; import { defer } from "./rsvp/defer";
import { config } from "./rsvp/config"; import { config } from "./rsvp/config";
import { resolve } from "./rsvp/resolve"; import { resolve } from "./rsvp/resolve";
...@@ -12,4 +13,4 @@ function configure(name, value) { ...@@ -12,4 +13,4 @@ function configure(name, value) {
config[name] = value; config[name] = value;
} }
export { Promise, EventTarget, all, hash, defer, denodeify, configure, resolve, reject }; export { Promise, EventTarget, all, hash, rethrow, defer, denodeify, configure, resolve, reject };
...@@ -51,7 +51,7 @@ function useMutationObserver() { ...@@ -51,7 +51,7 @@ function useMutationObserver() {
function useSetTimeout() { function useSetTimeout() {
return function(callback, arg) { return function(callback, arg) {
setTimeout(function() { global.setTimeout(function() {
callback(arg); callback(arg);
}, 1); }, 1);
}; };
......
var local = (typeof global === "undefined") ? this : global;
function rethrow(reason) {
local.setTimeout(function() {
throw reason;
});
throw reason;
}
export { rethrow };
...@@ -6,15 +6,15 @@ ...@@ -6,15 +6,15 @@
</head> </head>
<body> <body>
<div id="mocha"></div> <div id="mocha"></div>
<script src="../dist/rsvp-2.0.0.js"></script> <script src="../dist/rsvp-2.0.1.js"></script>
<script src="vendor/assert.js"></script> <script src="vendor/assert.js"></script>
<script src="vendor/mocha.js"></script> <script src="vendor/mocha.js"></script>
<script>mocha.setup({ ui: 'bdd', timeout: 200 })</script> <script>mocha.setup({ ui: 'bdd', timeout: 200 }); mocha.globals(['setTimeout']);</script>
<script src="../tmp/tests-bundle.js"></script> <script src="../tmp/tests-bundle.js"></script>
<script src="tests/extension_test.js"></script> <script src="tests/extension_test.js"></script>
<script> <script>
if (window.mochaPhantomJS) { mochaPhantomJS.run(); } if (window.mochaPhantomJS) { mochaPhantomJS.run(); }
else { mocha.run(); } else { mocha.globals(['setTimeout']); mocha.run(); }
</script> </script>
</body> </body>
</html> </html>
/*global describe, specify, it, assert */ /*global describe, specify, it, assert */
var local = (typeof global === "undefined") ? this : global;
if (typeof Object.getPrototypeOf !== "function") { if (typeof Object.getPrototypeOf !== "function") {
Object.getPrototypeOf = "".__proto__ === String.prototype Object.getPrototypeOf = "".__proto__ === String.prototype
? function (object) { ? function (object) {
...@@ -724,6 +726,47 @@ describe("RSVP extensions", function() { ...@@ -724,6 +726,47 @@ describe("RSVP extensions", function() {
}); });
}); });
describe("RSVP.rethrow", function() {
var onerror, oldSetTimeout = global.setTimeout;
after(function() {
global.setTimeout = oldSetTimeout;
});
it("should exist", function() {
assert(RSVP.rethrow);
});
it("rethrows an error", function(done) {
var thrownError = new Error('I am an error.');
local.setTimeout = function (callback) {
done();
var errorWasThrown = false;
try {
callback.call(this, arguments);
} catch(e) {
errorWasThrown = true;
}
assert(errorWasThrown, 'expect that an error was thrown');
};
function expectRejection(reason) {
assertEqual(reason, thrownError);
}
function doNotExpectFulfillment(value) {
done();
assert(false, value);
}
RSVP.reject(thrownError).
fail(RSVP.rethrow).
then(doNotExpectFulfillment, expectRejection);
});
});
describe("RSVP.resolve", function(){ describe("RSVP.resolve", function(){
specify("it should exist", function(){ specify("it should exist", function(){
assert(RSVP.resolve); assert(RSVP.resolve);
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment