Skip to content

Commit

Permalink
promise.yield and unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
briancavalier committed Nov 16, 2012
1 parent 18859e8 commit 88d63fb
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 5 deletions.
26 changes: 25 additions & 1 deletion test/promise.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,10 @@ assert = buster.assert;
refute = buster.refute;
fail = buster.assertions.fail;

var defer, isFrozen, undef;
var defer, isFrozen, undef, sentinel, other;

sentinel = {};
other = {};

defer = when.defer;

Expand Down Expand Up @@ -357,6 +360,27 @@ buster.testCase('promise', {

d.reject(1);
}
},

'yield': {
'should fulfill with the supplied value': function(done) {
when.resolve(other).yield(sentinel).then(
assert.same.bind(assert, sentinel)
).always(done);
},

'should fulfill with the value of a fulfilled promise': function(done) {
when.resolve(other).yield(when.resolve(sentinel)).then(
assert.same.bind(assert, sentinel)
).always(done);
},

'should reject with the reason of a rejected promise': function(done) {
when.resolve(other).yield(when.reject(sentinel)).then(
fail,
assert.same.bind(assert, sentinel)
).always(done);
}
}

});
Expand Down
20 changes: 16 additions & 4 deletions when.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
*/

(function(define) { 'use strict';
define(['module'], function () {
define(function () {
var reduceArray, slice, undef;

//
Expand Down Expand Up @@ -137,7 +137,6 @@ define(['module'], function () {
* Register a callback that will be called when a promise is
* resolved or rejected. Optionally also register a progress handler.
* Shortcut for .then(alwaysback, alwaysback, progback)
* @memberOf Promise
* @param alwaysback {Function}
* @param progback {Function}
* @return {Promise}
Expand All @@ -148,12 +147,25 @@ define(['module'], function () {

/**
* Register a rejection handler. Shortcut for .then(null, errback)
* @memberOf Promise
* @param errback {Function}
* @return {Promise}
*/
otherwise: function(errback) {
return this.then(undef, errback);
},

/**
* Shortcut for .then(function() { return value; })
* @param {*} value
* @return {Promise} a promise that:
* - is fulfilled if value is not a promise, or
* - if value is a promise, will fulfill with its value, or reject
* with its reason.
*/
yield: function(value) {
return this.then(function() {
return value;
});
}
};

Expand Down Expand Up @@ -717,7 +729,7 @@ define(['module'], function () {
});
})(typeof define == 'function' && define.amd
? define
: function (deps, factory) { typeof exports === 'object'
: function (factory) { typeof exports === 'object'
? (module.exports = factory())
: (this.when = factory());
}
Expand Down

0 comments on commit 88d63fb

Please sign in to comment.