Comparing version 1.0.1 to 1.0.2
var newPromise = require('deferred'); | ||
var _setTimeout = setTimeout; | ||
var _clearTimeout = clearTimeout; | ||
function newThen() { | ||
var def = newPromise(); | ||
var def = newPromise(); | ||
var parentFilename = getRequiringModulePath(); | ||
var timeout = 10; | ||
var timerId = _setTimeout(function() { | ||
console.error('aborting a promise in ' + parentFilename + ' that took more than ' + timeout + 's to fulfill'); | ||
throw new Error(); | ||
}, 10*1000); | ||
@@ -9,3 +17,3 @@ function promise(success, error) { | ||
return def.resolve(); | ||
if (success !== null ) | ||
if (success !== null ) | ||
return def.resolve(success); | ||
@@ -15,3 +23,3 @@ return def.reject(error); | ||
promise.then = function(success,error) { | ||
promise.then = function(success, error) { | ||
return def.promise.apply(def,arguments); | ||
@@ -21,2 +29,3 @@ }; | ||
promise.resolve = function() { | ||
_clearTimeout(timerId); | ||
return def.resolve.apply(def,arguments); | ||
@@ -26,2 +35,3 @@ }; | ||
promise.reject = function() { | ||
_clearTimeout(timerId); | ||
return def.reject.apply(def,arguments); | ||
@@ -31,5 +41,16 @@ }; | ||
return promise; | ||
} | ||
function getRequiringModulePath() { | ||
var path; | ||
var mod = module; | ||
//3 levels up: this file -> a_mock/index -> a/index -> requiring module | ||
for(var i = 0; i < 4; i++) { | ||
if(!mod.parent) return path; | ||
mod = mod.parent; | ||
path = mod.filename; | ||
} | ||
return path; | ||
} | ||
module.exports = newThen; | ||
module.exports = newThen; |
@@ -8,3 +8,3 @@ var assert = require('assert'); | ||
console.log('then'); | ||
var newResolver = requireMock('deferred'); | ||
@@ -24,5 +24,5 @@ var resolve = mock(); | ||
var arg = {}; | ||
var sut = require('../newThen')(); | ||
var sut = require('../index').then(); | ||
test('then should return promiseResult',function() { | ||
@@ -38,3 +38,3 @@ var expected = {}; | ||
assert.equal(sut.resolve(arg),expected); | ||
}); | ||
}); | ||
@@ -51,3 +51,3 @@ test('then reject should invoke resolver.reject',function() { | ||
assert.equal(sut(arg),expected); | ||
}); | ||
}); | ||
@@ -54,0 +54,0 @@ test('then implicit reject should invoke resolver.reject',function() { |
{ | ||
"name": "a_mock", | ||
"version": "1.0.1", | ||
"version": "1.0.2", | ||
"main": "index.js", | ||
@@ -8,3 +8,3 @@ "title": "a_mock", | ||
"keywords": ["mock","mocking","partial mock","strict mock","tdd","stub","stubbing","mock require","verify"], | ||
"dependencies": { | ||
"dependencies": { | ||
"deferred" : "0.7.1" | ||
@@ -15,3 +15,3 @@ }, | ||
"url" : "git@github.com:alfateam/a_mock.git" | ||
} | ||
} | ||
} |
135940
4347