Comparing version 0.3.0 to 0.3.1
@@ -17,2 +17,7 @@ module.exports = function (grunt) { | ||
} | ||
}, | ||
mithril: { | ||
files: { | ||
'promiz.mithril.min.js': ['promiz.mithril.js'] | ||
} | ||
} | ||
@@ -26,2 +31,2 @@ } | ||
}; | ||
}; |
{ | ||
"name": "promiz", | ||
"version": "0.3.0", | ||
"version": "0.3.1", | ||
"description": "A proper compact promise (promises/A+ spec compliant) library.", | ||
@@ -9,3 +9,3 @@ "main": "promiz.js", | ||
"spec-micro": "promises-aplus-tests test/spec-adapter-micro", | ||
"test": "jasmine-node test" | ||
"test": "jasmine-node test && promises-aplus-tests test/spec-adapter && promises-aplus-tests test/spec-adapter-micro" | ||
}, | ||
@@ -32,3 +32,4 @@ "repository": { | ||
"grunt": "~0.4.2", | ||
"grunt-contrib-uglify": "~0.2.7" | ||
"grunt-contrib-uglify": "~0.2.7", | ||
"jasmine-node": "~1.12.0" | ||
}, | ||
@@ -35,0 +36,0 @@ "author": "Zolmeister", |
@@ -5,3 +5,3 @@ (function () { | ||
} | ||
/** | ||
@@ -123,3 +123,3 @@ * @constructor | ||
self.val = v | ||
if (self.state === 'resolving' && typeof self.fn === 'function') { | ||
@@ -164,3 +164,3 @@ try { | ||
promise.prototype.done = function () { | ||
if (this.state = 'rejected' && !this.next) { | ||
if (this.state === 'rejected') { | ||
throw this.val | ||
@@ -202,3 +202,3 @@ } | ||
} | ||
promise.prototype.all = function() { | ||
@@ -212,14 +212,14 @@ var self = this | ||
} | ||
var cnt = 0 | ||
var target = list.length | ||
function done() { | ||
if (++cnt === target) p.resolve(list) | ||
} | ||
for(var i=0, l=list.length; i<l; i++) { | ||
var value = list[i] | ||
var ref; | ||
try { | ||
@@ -231,3 +231,3 @@ ref = value && value.then | ||
} | ||
(function(i){ | ||
@@ -295,3 +295,3 @@ self.thennable(ref, function(val){ | ||
} | ||
// Export our library object, either for node.js or as a globally scoped variable | ||
@@ -303,2 +303,2 @@ if (typeof module !== 'undefined') { | ||
} | ||
})() | ||
})() |
@@ -6,3 +6,3 @@ (function () { | ||
*/ | ||
function promise(fn, er) { | ||
function Deferred(fn, er) { | ||
// states | ||
@@ -28,2 +28,3 @@ // 0: pending | ||
} | ||
return this | ||
} | ||
@@ -38,14 +39,17 @@ | ||
} | ||
return this | ||
} | ||
self['then'] = function (fn, er) { | ||
var p = new promise(fn, er) | ||
next.push(p) | ||
var d = new Deferred(fn, er) | ||
if (state == 3) { | ||
p.resolve(val) | ||
d.resolve(val) | ||
} | ||
if (state == 4) { | ||
p.reject(val) | ||
else if (state == 4) { | ||
d.reject(val) | ||
} | ||
return p | ||
else { | ||
next.push(d) | ||
} | ||
return d | ||
} | ||
@@ -55,14 +59,5 @@ | ||
state = type || 4 | ||
if (state == 3) { | ||
next.map(function (p) { | ||
p.resolve(val) | ||
}) | ||
} | ||
if (state == 4) { | ||
next.map(function (p) { | ||
p.reject(val) | ||
}) | ||
} | ||
next.map(function (p) { | ||
state == 3 && p.resolve(val) || p.reject(val) | ||
}) | ||
} | ||
@@ -72,3 +67,3 @@ | ||
// cb, ec, cn : successCallback, failureCallback, notThennableCallback | ||
var thennable = function (ref, cb, ec, cn) { | ||
function thennable (ref, cb, ec, cn) { | ||
if (typeof val == 'object' && typeof ref == 'function') { | ||
@@ -121,3 +116,3 @@ try { | ||
if (state == 2 && typeof er == 'function') { | ||
else if (state == 2 && typeof er == 'function') { | ||
val = er(val) | ||
@@ -146,16 +141,8 @@ state = 1 | ||
// this object gets globalalized/exported | ||
var promiz = { | ||
// promise factory | ||
defer: function () { | ||
return new promise() | ||
} | ||
} | ||
// Export our library object, either for node.js or as a globally scoped variable | ||
if (typeof module != 'undefined') { | ||
module['exports'] = promiz | ||
module['exports'] = Deferred | ||
} else { | ||
this['Promiz'] = promiz | ||
this['Promiz'] = Deferred | ||
} | ||
})() | ||
})() |
@@ -1,1 +0,1 @@ | ||
!function(){function a(b,c){function d(){var a;try{a=g&&g.then}catch(h){return g=h,f=2,d()}j(a,function(){f=1,d()},function(){f=2,d()},function(){try{1==f&&"function"==typeof b&&(g=b(g)),2==f&&"function"==typeof c&&(g=c(g),f=1)}catch(d){return g=d,i()}g==e?(g=TypeError(),i()):j(a,function(){i(3)},i,function(){i(1==f&&3)})})}var e=this,f=0,g=0,h=[];e.promise=e,e.resolve=function(a){f||(g=a,f=1,setTimeout(d))},e.reject=function(a){f||(g=a,f=2,setTimeout(d))},e.then=function(b,c){var d=new a(b,c);return h.push(d),3==f&&d.resolve(g),4==f&&d.reject(g),d};var i=function(a){f=a||4,3==f&&h.map(function(a){a.resolve(g)}),4==f&&h.map(function(a){a.reject(g)})},j=function(a,b,c,d){if("object"==typeof g&&"function"==typeof a)try{var e=0;a.call(g,function(a){e++||(g=a,b())},function(a){e++||(g=a,c())})}catch(f){g=f,c()}else d()}}var b={defer:function(){return new a}};"undefined"!=typeof module?module.exports=b:this.Promiz=b}(); | ||
!function(){function a(b,c){function d(a,b,c,d){if("object"==typeof h&&"function"==typeof a)try{var e=0;a.call(h,function(a){e++||(h=a,b())},function(a){e++||(h=a,c())})}catch(f){h=f,c()}else d()}function e(){var a;try{a=h&&h.then}catch(i){return h=i,g=2,e()}d(a,function(){g=1,e()},function(){g=2,e()},function(){try{1==g&&"function"==typeof b?h=b(h):2==g&&"function"==typeof c&&(h=c(h),g=1)}catch(e){return h=e,j()}h==f?(h=TypeError(),j()):d(a,function(){j(3)},j,function(){j(1==g&&3)})})}var f=this,g=0,h=0,i=[];f.promise=f,f.resolve=function(a){return g||(h=a,g=1,setTimeout(e)),this},f.reject=function(a){return g||(h=a,g=2,setTimeout(e)),this},f.then=function(b,c){var d=new a(b,c);return 3==g?d.resolve(h):4==g?d.reject(h):i.push(d),d};var j=function(a){g=a||4,i.map(function(a){3==g&&a.resolve(h)||a.reject(h)})}}"undefined"!=typeof module?module.exports=a:this.Promiz=a}(); |
@@ -1,1 +0,1 @@ | ||
!function(){function a(a,b){var c=this;c.promise=c,c.state="pending",c.val=null,c.fn=a||null,c.er=b||null,c.next=[]}var b="undefined"!=typeof setImmediate?setImmediate:function(a){setTimeout(a,0)};a.prototype.resolve=function(a){var c=this;"pending"===c.state&&(c.val=a,c.state="resolving",b(function(){c.fire()}))},a.prototype.reject=function(a){var c=this;"pending"===c.state&&(c.val=a,c.state="rejecting",b(function(){c.fire()}))},a.prototype.then=function(b,c){var d=this,e=new a(b,c);return d.next.push(e),"resolved"===d.state&&e.resolve(d.val),"rejected"===d.state&&e.reject(d.val),e},a.prototype.fail=function(a){return this.then(null,a)},a.prototype.finish=function(a){var b=this;b.state=a,"resolved"===b.state&&b.next.map(function(a){a.resolve(b.val)}),"rejected"===b.state&&b.next.map(function(a){a.reject(b.val)})},a.prototype.thennable=function(a,b,c,d,e){var f=this;if(e=e||f.val,"object"==typeof e&&"function"==typeof a)try{var g=0;a.call(e,function(a){0===g++&&b(a)},function(a){0===g++&&c(a)})}catch(h){c(h)}else d(e)},a.prototype.fire=function(){var a,b=this;try{a=b.val&&b.val.then}catch(c){return b.val=c,b.state="rejecting",b.fire()}b.thennable(a,function(a){b.val=a,b.state="resolving",b.fire()},function(a){b.val=a,b.state="rejecting",b.fire()},function(c){if(b.val=c,"resolving"===b.state&&"function"==typeof b.fn)try{b.val=b.fn.call(void 0,b.val)}catch(d){return b.val=d,b.finish("rejected")}if("rejecting"===b.state&&"function"==typeof b.er)try{b.val=b.er.call(void 0,b.val),b.state="resolving"}catch(d){return b.val=d,b.finish("rejected")}return b.val===b?(b.val=TypeError(),b.finish("rejected")):(b.thennable(a,function(a){b.val=a,b.finish("resolved")},function(a){b.val=a,b.finish("rejected")},function(a){b.val=a,"resolving"===b.state?b.finish("resolved"):b.finish("rejected")}),void 0)})},a.prototype.done=function(){if(this.state="rejected"&&!this.next)throw this.val;return null},a.prototype.nodeify=function(a){return"function"==typeof a?this.then(function(b){try{a(null,b)}catch(c){setImmediate(function(){throw c})}return b},function(b){try{a(b)}catch(c){setImmediate(function(){throw c})}return b}):this},a.prototype.spread=function(a,b){return this.all().then(function(b){return"function"==typeof a&&a.apply(null,b)},b)},a.prototype.all=function(){var b=this;return this.then(function(c){function d(){++f===g&&e.resolve(c)}var e=new a;if(!(c instanceof Array))return e.reject(TypeError),e;for(var f=0,g=c.length,h=0,i=c.length;i>h;h++){var j,k=c[h];try{j=k&&k.then}catch(l){e.reject(l);break}!function(a){b.thennable(j,function(b){c[a]=b,d()},function(b){c[a]=b,d()},function(){d()},k)}(h)}return e})};var c={defer:function(){return new a(null,null)},fcall:function(){var b=new a,c=Array.apply([],arguments),d=c.shift();try{var e=d.apply(null,c);b.resolve(e)}catch(f){b.reject(f)}return b},nfcall:function(){var b=new a,c=Array.apply([],arguments),d=c.shift();try{c.push(function(a,c){return a?b.reject(a):b.resolve(c)}),d.apply(null,c)}catch(e){b.reject(e)}return b}};"undefined"!=typeof module?module.exports=c:self.Promiz=c}(); | ||
!function(){function a(a,b){var c=this;c.promise=c,c.state="pending",c.val=null,c.fn=a||null,c.er=b||null,c.next=[]}var b="undefined"!=typeof setImmediate?setImmediate:function(a){setTimeout(a,0)};a.prototype.resolve=function(a){var c=this;"pending"===c.state&&(c.val=a,c.state="resolving",b(function(){c.fire()}))},a.prototype.reject=function(a){var c=this;"pending"===c.state&&(c.val=a,c.state="rejecting",b(function(){c.fire()}))},a.prototype.then=function(b,c){var d=this,e=new a(b,c);return d.next.push(e),"resolved"===d.state&&e.resolve(d.val),"rejected"===d.state&&e.reject(d.val),e},a.prototype.fail=function(a){return this.then(null,a)},a.prototype.finish=function(a){var b=this;b.state=a,"resolved"===b.state&&b.next.map(function(a){a.resolve(b.val)}),"rejected"===b.state&&b.next.map(function(a){a.reject(b.val)})},a.prototype.thennable=function(a,b,c,d,e){var f=this;if(e=e||f.val,"object"==typeof e&&"function"==typeof a)try{var g=0;a.call(e,function(a){0===g++&&b(a)},function(a){0===g++&&c(a)})}catch(h){c(h)}else d(e)},a.prototype.fire=function(){var a,b=this;try{a=b.val&&b.val.then}catch(c){return b.val=c,b.state="rejecting",b.fire()}b.thennable(a,function(a){b.val=a,b.state="resolving",b.fire()},function(a){b.val=a,b.state="rejecting",b.fire()},function(c){if(b.val=c,"resolving"===b.state&&"function"==typeof b.fn)try{b.val=b.fn.call(void 0,b.val)}catch(d){return b.val=d,b.finish("rejected")}if("rejecting"===b.state&&"function"==typeof b.er)try{b.val=b.er.call(void 0,b.val),b.state="resolving"}catch(d){return b.val=d,b.finish("rejected")}return b.val===b?(b.val=TypeError(),b.finish("rejected")):void b.thennable(a,function(a){b.val=a,b.finish("resolved")},function(a){b.val=a,b.finish("rejected")},function(a){b.val=a,b.finish("resolving"===b.state?"resolved":"rejected")})})},a.prototype.done=function(){if(this.state="rejected"&&!this.next)throw this.val;return null},a.prototype.nodeify=function(a){return"function"==typeof a?this.then(function(b){try{a(null,b)}catch(c){setImmediate(function(){throw c})}return b},function(b){try{a(b)}catch(c){setImmediate(function(){throw c})}return b}):this},a.prototype.spread=function(a,b){return this.all().then(function(b){return"function"==typeof a&&a.apply(null,b)},b)},a.prototype.all=function(){var b=this;return this.then(function(c){function d(){++f===g&&e.resolve(c)}var e=new a;if(!(c instanceof Array))return e.reject(TypeError),e;for(var f=0,g=c.length,h=0,i=c.length;i>h;h++){var j,k=c[h];try{j=k&&k.then}catch(l){e.reject(l);break}!function(a){b.thennable(j,function(b){c[a]=b,d()},function(b){c[a]=b,d()},function(){d()},k)}(h)}return e})};var c={defer:function(){return new a(null,null)},fcall:function(){var b=new a,c=Array.apply([],arguments),d=c.shift();try{var e=d.apply(null,c);b.resolve(e)}catch(f){b.reject(f)}return b},nfcall:function(){var b=new a,c=Array.apply([],arguments),d=c.shift();try{c.push(function(a,c){return a?b.reject(a):b.resolve(c)}),d.apply(null,c)}catch(e){b.reject(e)}return b}};"undefined"!=typeof module?module.exports=c:self.Promiz=c}(); |
 | ||
====== | ||
<a href="http://promises-aplus.github.com/promises-spec"> | ||
<img src="http://promises-aplus.github.com/promises-spec/assets/logo-small.png" | ||
<img src="https://raw.github.com/Zolmeister/promiz/master/imgs/promise-logo-small.png" | ||
align="right" alt="Promises/A+ logo" /> | ||
</a> | ||
A proper compact promise (promises/A+ spec compliant) library. ([How it was built](http://www.zolmeister.com/2013/07/promizjs.html)) | ||
A proper compact promise (promises/A+ spec compliant) library. ([Blog Post](http://www.zolmeister.com/2014/01/promiz-micro-promises-in-228-bytes.html)) ~~([How it was built](http://www.zolmeister.com/2013/07/promizjs.html))~~ | ||
 | ||
## Install | ||
@@ -18,3 +19,3 @@ ```bash | ||
Promiz: **590 bytes** (min + gzip) - as reported by uglify.js | ||
Promiz Micro: **238 bytes** (min + gzip) - as reported by uglify.js | ||
Promiz Micro: **228 bytes** (min + gzip) - as reported by uglify.js | ||
## What are promises? | ||
@@ -46,3 +47,3 @@ ```javascript | ||
}).all().then(function(list){ | ||
// list === [11, 33, 55] | ||
// list === [11, 33, 55]c | ||
// Yeah, try doing that with async (obviously you could, but it would look hideous) | ||
@@ -72,3 +73,3 @@ // and just because we can | ||
} else { | ||
// This throws an error, which can be caught by .catch() or .done() | ||
// This throws an error, which can be caught by .fail() or .done() | ||
deferred.reject(new Error('incorrect input')) | ||
@@ -149,7 +150,7 @@ } | ||
### (Promises/A+ spec compliant) | ||
#### Promiz.defer() | ||
#### new Promiz() | ||
```javascript | ||
function testPromise(val) { | ||
// create a new instance of a deferred object (a `promise`) | ||
var deferred = Promiz.defer() | ||
var deferred = new Promiz() | ||
setTimeout(function(){ | ||
@@ -159,3 +160,3 @@ if (val === 42) { | ||
} else { | ||
// This throws an error, which can be caught by .catch() or .done() | ||
// This throws an error, which can be caught by .fail() or .done() | ||
deferred.reject(new Error('incorrect input')) | ||
@@ -167,2 +168,12 @@ } | ||
testPromise(42).then() | ||
// or better yet | ||
new Promiz().then(function(x){ | ||
console.log(x+' that') | ||
return 'just happened' | ||
}) | ||
.resolve('yeah') | ||
.then(function(x){ | ||
console.log(x) | ||
}) | ||
``` | ||
@@ -169,0 +180,0 @@ #### .then(:success, :error (optional)) |
@@ -1,5 +0,5 @@ | ||
var Promiz = require('./../promiz.micro'); | ||
var Deferred = require('./../promiz.micro'); | ||
module.exports = { | ||
resolved: function (value) { | ||
var d = Promiz.defer(); | ||
var d = new Deferred(); | ||
d.resolve(value); | ||
@@ -9,7 +9,7 @@ return d; | ||
rejected: function (error) { | ||
var d = Promiz.defer(); | ||
var d = new Deferred(); | ||
d.reject(error); | ||
return d; | ||
}, | ||
deferred: Promiz.defer | ||
deferred: function () { return new Deferred(); }, | ||
}; |
Sorry, the diff of this file is not supported yet
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
629705
22
1206
184
0
4