Comparing version 0.0.0 to 0.0.1
{ | ||
"name": "promiz", | ||
"version": "0.0.0", | ||
"version": "0.0.1", | ||
"description": "A proper compact promise (promises/A+ spec compliant) library.", | ||
@@ -5,0 +5,0 @@ "main": "promiz.js", |
104
promiz.js
@@ -7,8 +7,9 @@ var promiz = (function(){ | ||
this.stack = [] | ||
this.throwing = false | ||
this.resolve = function(val){ | ||
process.nextTick(function(){ | ||
if (this.state === 'pending'){ | ||
this.state = 'resolved' | ||
this.fire(val) | ||
}.bind(this)) | ||
} | ||
return this | ||
@@ -18,6 +19,6 @@ } | ||
this.reject = function(val){ | ||
process.nextTick(function(){ | ||
if (this.state === 'pending'){ | ||
this.state = 'rejected' | ||
this.fire(val) | ||
}.bind(this)) | ||
} | ||
return this | ||
@@ -27,7 +28,5 @@ } | ||
this.then = function(fn, er){ | ||
this.stack.push([fn, er]) | ||
this.stack.push([fn || function(){}, er]) | ||
if (this.state !== 'pending') { | ||
process.nextTick(function(){ | ||
this.fire() | ||
}.bind(this)) | ||
} | ||
@@ -38,3 +37,11 @@ return this | ||
this.done = function(){ | ||
// TODO | ||
this.throwing = true | ||
if (this.state !== 'pending') { | ||
this.fire() | ||
} | ||
return void 0 | ||
} | ||
this.throws = function(){ | ||
this.throwing = true | ||
return this | ||
@@ -44,22 +51,71 @@ } | ||
this.catch = function (fn) { | ||
this.stack.push([null, fn]) | ||
this.stack.push([null, fn || function(){}]) | ||
if (this.state !== 'pending') { | ||
process.nextTick(function(){ | ||
this.fire() | ||
}.bind(this)) | ||
} | ||
return this | ||
} | ||
this.fail = function (fn) { | ||
return this.catch(fn) | ||
} | ||
this.nodeify = function () { | ||
// TODO | ||
} | ||
this.spread = function (fn, er) { | ||
return this.all().then(function(list){ | ||
return fn ? fn.apply(void 0, list) : null | ||
}, er) | ||
} | ||
} | ||
this.all = function () { | ||
// TODO | ||
var self = this | ||
this.stack.push([function(list){ | ||
list = list instanceof Array ? list : [] | ||
if (list.length === 0){ | ||
return list | ||
} | ||
self.state = 'pending' | ||
var cnt = 0 | ||
var errored = false | ||
function checkDone(){ | ||
if(cnt !== list.length) { | ||
return | ||
} | ||
self.resolve(list) | ||
} | ||
list.forEach(function(val, i){ | ||
if(val && val.then){ | ||
val.then(function(res){ | ||
list[i] = res | ||
cnt++ | ||
checkDone() | ||
}, function(err){ | ||
self.reject(err) | ||
}) | ||
} else { | ||
list[i] = val | ||
cnt++ | ||
checkDone() | ||
} | ||
}) | ||
return list | ||
}, null]) | ||
if (this.state !== 'pending') { | ||
this.fire() | ||
} | ||
return this | ||
} | ||
@@ -78,22 +134,16 @@ | ||
val = this.value = fn.call(void 0, val) | ||
if(val && val.then && (val.catch || val.fail)) { | ||
if(val && val.then) { | ||
var previousState = this.state | ||
this.state = 'pending' | ||
var promise = val.then(function(v){ | ||
val = this.value = v | ||
this.resolve(v) | ||
}.bind(this)) | ||
var catcher = function(err){ | ||
}.bind(this), function(err){ | ||
val = this.value = err | ||
if(previousState !== 'rejected') this.stack.unshift(entry) | ||
this.reject(err) | ||
}.bind(this) | ||
}.bind(this)) | ||
if (promise.catch) { | ||
promise.catch(catcher) | ||
} else { | ||
promise.fail(catcher) | ||
} | ||
} else if (this.state === 'rejected') { | ||
@@ -109,2 +159,6 @@ this.state = 'resolved' | ||
} | ||
if(this.throwing && this.stack.length === 0 && this.state === 'rejected') { | ||
throw this.value | ||
} | ||
} | ||
@@ -134,2 +188,2 @@ | ||
module.exports = promiz | ||
module.exports = promiz |
@@ -7,3 +7,3 @@ var promiz = require('./promiz') | ||
describe('basic user deferreds', function(){ | ||
return | ||
function testPromise() { | ||
@@ -113,6 +113,20 @@ var deferred = promiz.defer() | ||
it('supports double rejects', function(done) { | ||
function doubleReject() { | ||
var deferred = promiz.defer() | ||
deferred.reject(new Error('abc')) | ||
deferred.reject(new Error('def')) | ||
return deferred | ||
} | ||
promise.then(doubleReject).fail(function(abc){ | ||
expect(abc.message).toBe('abc') | ||
done() | ||
}) | ||
}) | ||
}) | ||
describe('error handling', function(){ | ||
return | ||
function errDefer() { | ||
@@ -182,2 +196,73 @@ var deferred = promiz.defer() | ||
describe('throwers', function(){ | ||
return | ||
function testPromise() { | ||
var deferred = promiz.defer() | ||
process.nextTick(function(){ | ||
deferred.resolve(22) | ||
}) | ||
return deferred | ||
} | ||
var promise = testPromise() | ||
it('prevents continuation on done', function(){ | ||
expect(promise.done()).toBeUndefined() | ||
expect(promise.throwing).toBe(true) | ||
}) | ||
it('sets throwing to true', function(){ | ||
promise = testPromise() | ||
promise.throws() | ||
expect(promise.throwing).toBe(true) | ||
}) | ||
}) | ||
describe('spread and all', function(){ | ||
function testPromise() { | ||
var deferred = promiz.defer() | ||
process.nextTick(function(){ | ||
deferred.resolve(22) | ||
}) | ||
return deferred | ||
} | ||
var promise = testPromise() | ||
it('alls', function(done){ | ||
promise.then(function(){ | ||
return [1,2,3] | ||
}) | ||
.all() | ||
.then(function(oneTwoThree){ | ||
expect(oneTwoThree.join('')).toBe('123') | ||
var prom1 = testPromise() | ||
var prom2 = testPromise().then() | ||
var prom3 = testPromise().then(function(){ return 33 }) | ||
var static = 77 | ||
return [prom1, prom2, prom3, static] | ||
}).all().then(function(list){ | ||
expect(list[0]).toBe(22) | ||
expect(list[1]).toBeUndefined() | ||
expect(list[2]).toBe(33) | ||
expect(list[3]).toBe(77) | ||
done() | ||
}) | ||
}) | ||
it('spreads', function(){ | ||
}) | ||
}) | ||
describe('asyncronicity', function(){ | ||
it('is actually always asyncronouse', function(){ | ||
}) | ||
}) | ||
}) | ||
@@ -184,0 +269,0 @@ |
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
12006
367