Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Add `P.reject()`.

`.reject()` constructs a rejected promise.
  • Loading branch information...
commit 13f9ad2f9d98b3f2b99adbb3fcf97c0e587e8c94 1 parent c795a40
@kitcambridge kitcambridge authored
Showing with 16 additions and 1 deletion.
  1. +5 −0 p.js
  2. +1 −1  p.min.js
  3. +10 −0 test/test.js
View
5 p.js
@@ -232,6 +232,11 @@
return resolverFor( new Promise() );
}
+ P.reject = reject;
+ function reject( reason ) {
+ return Settle( new Promise(), REJECTED, reason );
+ }
+
function Promise() {
this._state = 0;
this._value = void 0;
View
2  p.min.js
@@ -1 +1 @@
-(function(e){if(typeof module!=="undefined"&&module&&module.exports){module.exports=e()}else if(typeof define==="function"&&define.amd){define(e)}else{P=e()}})(function(){"use strict";var e={f:null,n:null},t=e,n=false,r,i,o=c(typeof window)&&window||c(typeof worker)&&worker,u=c.call,f=c.apply;function a(){while(e.n){e=e.n;var t=e.f;e.f=null;t()}n=false}var s=function(e){t=t.n={f:e,n:null};if(!n){n=true;i(a,0)}};function c(e){return e==="object"||e==="function"}if(c(typeof process)&&process&&process.nextTick){i=process.nextTick}else if(c(typeof setImmediate)){i=o?function(e){o.setImmediate(e)}:function(e){setImmediate(e)}}else if(c(typeof MessageChannel)){r=new MessageChannel;r.port1.onmessage=a;i=function(){r.port2.postMessage(0)}}else{i=setTimeout;if(o&&c(typeof Image)&&Image){(function(){var e=0;var t=function(e){var t=new Image;t.onerror=e;t.src="data:image/png,"};try{t(function(){if(--e===0){i=t}});++e}catch(n){}e&&setTimeout(function(){e=0},0)})()}}function l(e,t){for(var n=0,r=e.length;n<r;++n){if(n in e){t(e[n],n)}}}function p(e){try{if(_.onerror){_.onerror(e)}else{throw e}}catch(t){setTimeout(function(){throw t},0)}}var v=0;var h=1;var d=2;function _(e){return e instanceof j?e:w(new j,e)}function m(e,t,n){if(e._state){return e}e._state=t;e._value=n;if(e._pending.length>0){l(e._pending,s)}e._pending=null;return e}function y(e,t){e._pending.push(t)}function w(e,t){if(e._state){return e}if(t instanceof j){if(t===e){m(e,d,new TypeError("You can't resolve a promise with itself"))}else if(t._state){m(e,t._state,t._value)}else{y(t,function(){m(e,t._state,t._value)})}}else if(t!==Object(t)){m(e,h,t)}else{s(function(){var n=g(e);try{var r=t.then;if(typeof r==="function"){u.call(r,t,n.resolve,n.reject)}else{m(e,h,t)}}catch(i){n.reject(i)}})}return e}function g(e){var t=false;return{promise:e,resolve:function(n){if(!t){t=true;w(e,n)}},reject:function(n){if(!t){t=true;m(e,d,n)}}}}_.defer=T;function T(){return g(new j)}function j(){this._state=0;this._value=void 0;this._pending=[]}j.prototype.then=function(e,t){var n=typeof e==="function"?e:null;var r=typeof t==="function"?t:null;var i=this;var o=new j;function u(){var e,t=i._state===h?n:r;if(t!==null){try{e=t(i._value)}catch(u){m(o,d,u);return}w(o,e)}else{m(o,i._state,i._value)}}if(i._state===v){y(i,u)}else{s(u)}return o};j.prototype.done=function(e,t){var n=this;if(e||t){n=n.then(e,t)}n.then(null,p)};j.prototype.fail=function(e){return this.then(null,e)};j.prototype.spread=function(e,t){return this.then(e&&function(n){return x(n,[]).then(function(t){return f.call(e,void 0,t)},t)},t)};j.prototype.timeout=function(e,t){var n=this;var r=new j;if(n._state!==v){m(r,n._state,n._value)}else{var i=setTimeout(function(){m(r,d,new Error(t||"Timed out after "+e+" ms"))},e);y(n,function(){clearTimeout(i);m(r,n._state,n._value)})}return r};j.prototype.delay=function(e){var t=T();this.then(function(n){setTimeout(function(){t.resolve(n)},e)},t.reject);return t.promise};j.prototype.inspect=function(){switch(this._state){case v:return{state:"pending"};case h:return{state:"fulfilled",value:this._value};case d:return{state:"rejected",reason:this._value};default:throw new TypeError("invalid state")}};function I(e){function t(t){return e(t,[])}function n(e){return _(e).then(t)}n._=e;return n}_.allSettled=I(k);function k(e,t){var n=0;var r=new j;l(e,function(e,i){var o=_(e);if(o._state===v){++n;y(o,function(){t[i]=o.inspect();if(--n===0){m(r,h,t)}})}else{t[i]=o.inspect()}});if(n===0){m(r,h,t)}return r}_.all=I(x);function x(e,t){var n=0;var r=T();l(e,function(e,i){var o=_(e);if(o._state===h){t[i]=o._value}else{++n;o.then(function(e){t[i]=e;if(--n===0){r.resolve(t)}},r.reject)}});if(n===0){r.resolve(t)}return r.promise}_.promised=E;function E(e){function t(t){return f.apply(e,t)}return function(){var e=x(arguments,[]);return x([this,e],[]).then(t)}}_.onerror=null;_.nextTick=function(e){s(function(){try{e()}catch(t){setTimeout(function(){throw t},0)}})};return _});
+!function(e){if(typeof module!=="undefined"&&module&&module.exports){module.exports=e()}else if(typeof define==="function"&&define.amd){define(e)}else{P=e()}}(function(){"use strict";var e={f:null,n:null},t=e,n=false,r,i,o=c(typeof window)&&window||c(typeof worker)&&worker,u=c.call,f=c.apply;function a(){while(e.n){e=e.n;var t=e.f;e.f=null;t()}n=false}var s=function(e){t=t.n={f:e,n:null};if(!n){n=true;i(a,0)}};function c(e){return e==="object"||e==="function"}if(c(typeof process)&&process&&process.nextTick){i=process.nextTick}else if(c(typeof setImmediate)){i=o?function(e){o.setImmediate(e)}:function(e){setImmediate(e)}}else if(c(typeof MessageChannel)){r=new MessageChannel;r.port1.onmessage=a;i=function(){r.port2.postMessage(0)}}else{i=setTimeout;if(o&&c(typeof Image)&&Image){!function(){var e=0;var t=function(e){var t=new Image;t.onerror=e;t.src="data:image/png,"};try{t(function(){if(--e===0){i=t}});++e}catch(n){}e&&setTimeout(function(){e=0},0)}()}}function l(e,t){for(var n=0,r=e.length;n<r;++n){if(n in e){t(e[n],n)}}}function p(e){try{if(_.onerror){_.onerror(e)}else{throw e}}catch(t){setTimeout(function(){throw t},0)}}var v=0;var h=1;var d=2;function _(e){return e instanceof I?e:w(new I,e)}function m(e,t,n){if(e._state){return e}e._state=t;e._value=n;if(e._pending.length>0){l(e._pending,s)}e._pending=null;return e}function y(e,t){e._pending.push(t)}function w(e,t){if(e._state){return e}if(t instanceof I){if(t===e){m(e,d,new TypeError("You can't resolve a promise with itself"))}else if(t._state){m(e,t._state,t._value)}else{y(t,function(){m(e,t._state,t._value)})}}else if(t!==Object(t)){m(e,h,t)}else{s(function(){var n=g(e);try{var r=t.then;if(typeof r==="function"){u.call(r,t,n.resolve,n.reject)}else{m(e,h,t)}}catch(i){n.reject(i)}})}return e}function g(e){var t=false;return{promise:e,resolve:function(n){if(!t){t=true;w(e,n)}},reject:function(n){if(!t){t=true;m(e,d,n)}}}}_.defer=T;function T(){return g(new I)}_.reject=j;function j(e){return m(new I,d,e)}function I(){this._state=0;this._value=void 0;this._pending=[]}I.prototype.then=function(e,t){var n=typeof e==="function"?e:null;var r=typeof t==="function"?t:null;var i=this;var o=new I;function u(){var e,t=i._state===h?n:r;if(t!==null){try{e=t(i._value)}catch(u){m(o,d,u);return}w(o,e)}else{m(o,i._state,i._value)}}if(i._state===v){y(i,u)}else{s(u)}return o};I.prototype.done=function(e,t){var n=this;if(e||t){n=n.then(e,t)}n.then(null,p)};I.prototype.fail=function(e){return this.then(null,e)};I.prototype.spread=function(e,t){return this.then(e&&function(n){return E(n,[]).then(function(t){return f.call(e,void 0,t)},t)},t)};I.prototype.timeout=function(e,t){var n=this;var r=new I;if(n._state!==v){m(r,n._state,n._value)}else{var i=setTimeout(function(){m(r,d,new Error(t||"Timed out after "+e+" ms"))},e);y(n,function(){clearTimeout(i);m(r,n._state,n._value)})}return r};I.prototype.delay=function(e){var t=T();this.then(function(n){setTimeout(function(){t.resolve(n)},e)},t.reject);return t.promise};I.prototype.inspect=function(){switch(this._state){case v:return{state:"pending"};case h:return{state:"fulfilled",value:this._value};case d:return{state:"rejected",reason:this._value};default:throw new TypeError("invalid state")}};function k(e){function t(t){return e(t,[])}function n(e){return _(e).then(t)}n._=e;return n}_.allSettled=k(x);function x(e,t){var n=0;var r=new I;l(e,function(e,i){var o=_(e);if(o._state===v){++n;y(o,function(){t[i]=o.inspect();if(--n===0){m(r,h,t)}})}else{t[i]=o.inspect()}});if(n===0){m(r,h,t)}return r}_.all=k(E);function E(e,t){var n=0;var r=T();l(e,function(e,i){var o=_(e);if(o._state===h){t[i]=o._value}else{++n;o.then(function(e){t[i]=e;if(--n===0){r.resolve(t)}},r.reject)}});if(n===0){r.resolve(t)}return r.promise}_.promised=M;function M(e){function t(t){return f.apply(e,t)}return function(){var e=E(arguments,[]);return E([this,e],[]).then(t)}}_.onerror=null;_.nextTick=function(e){s(function(){try{e()}catch(t){setTimeout(function(){throw t},0)}})};return _});
View
10 test/test.js
@@ -110,6 +110,16 @@ describe("inspect", function() {
});
});
+describe("reject", function() {
+
+ it("returns a rejected promise", function() {
+ var theReason = new Error("Rejection");
+ return P.reject(theReason).then( fail, function ( reason ) {
+ expect( reason ).to.be(theReason);
+ });
+ });
+});
+
describe("all", function() {
it("resolves when passed an empty array", function() {
Please sign in to comment.
Something went wrong with that request. Please try again.