Permalink
Browse files

already-resolved deferreds return a promise for the passed-in value w…

…hen resolved or rejected again. This makes them indistinguishable from unresolved deferreds
  • Loading branch information...
1 parent 18c7132 commit f0027527ae8a7987da6a5bea13aae5ee7421c5fe @briancavalier briancavalier committed Jun 27, 2012
Showing with 34 additions and 30 deletions.
  1. +32 −29 test/defer.js
  2. +2 −1 when.js
View
61 test/defer.js
@@ -1,8 +1,9 @@
(function(buster, when) {
-var assert, fail;
+var assert, refute, fail;
assert = buster.assert;
+refute = buster.refute;
fail = buster.assertions.fail;
function fakeResolved(val) {
@@ -242,58 +243,60 @@ buster.testCase('when.defer', {
}
},
- 'should throw if resolved when already resolved': function() {
+ 'should return a promise for passed-in resolution value when already resolved': function(done) {
var d = when.defer();
d.resolve(1);
- assert.exception(function() {
- d.resolve();
- });
+ d.resolve(2).then(function(val) {
+ assert.equals(val, 2);
+ }).always(done);
},
- 'should throw if rejected when already resolved': function() {
+ 'should return a promise for passed-in rejection value when already resolved': function(done) {
var d = when.defer();
d.resolve(1);
- assert.exception(function() {
- d.reject();
- });
+ d.reject(2).then(
+ fail,
+ function(val) {
+ assert.equals(val, 2);
+ }
+ ).always(done);
},
- 'should throw on progress when already resolved': function() {
+ 'should return silently on progress when already resolved': function() {
var d = when.defer();
d.resolve(1);
- assert.exception(function() {
- d.progress();
- });
+ refute.defined(d.progress());
},
- 'should throw if resolved when already rejected': function() {
+ 'should return a promise for passed-in resolution value when already rejected': function(done) {
var d = when.defer();
- d.resolve(1);
+ d.reject(1);
- assert.exception(function() {
- d.resolve();
- });
+ d.resolve(2).then(function(val) {
+ assert.equals(val, 2);
+ }).always(done);
},
- 'should throw if rejected when already rejected': function() {
+ 'should return a promise for passed-in rejection value when already rejected': function(done) {
var d = when.defer();
- d.resolve(1);
-
- assert.exception(function() {
- d.reject();
- });
+ d.reject(1);
+
+ d.reject(2).then(
+ fail,
+ function(val) {
+ assert.equals(val, 2);
+ }
+ ).always(done);
},
- 'should throw on progress when already rejected': function() {
+ 'should return silently on progress when already rejected': function() {
var d = when.defer();
- d.resolve(1);
+ d.reject(1);
- assert.exception(function() {
- d.progress();
- });
+ refute.defined(d.progress());
}
});
View
3 when.js
@@ -343,7 +343,8 @@ define(function() { "use strict";
// Replace _then with one that directly notifies with the result.
// Replace _resolve so that this Deferred can only be resolved once
_then = value.then;
- _resolve = _progress = noop;
+ _resolve = resolve;
+ _progress = noop;
// Notify listeners
while (listener = listeners[i++]) listener(value);

0 comments on commit f002752

Please sign in to comment.