promise-timeout
Advanced tools
Comparing version 1.0.0 to 1.1.0
# promise-timeout | ||
## v1.1.0 (2017-03-07) | ||
* Updated mocha. | ||
* #1 - Clear timeout when promise settles, to avoid issues blocking the | ||
event loop from exiting. | ||
## v1.0.0 (2015-11-18) | ||
* Initial release |
16
index.js
@@ -1,2 +0,2 @@ | ||
// Copyright (c) 2015 David M. Lee, II | ||
// Copyright (c) 2015-2017 David M. Lee, II | ||
'use strict'; | ||
@@ -23,10 +23,18 @@ | ||
module.exports.timeout = function(promise, timeoutMillis) { | ||
let timeout; | ||
return Promise.race([ | ||
promise, | ||
new Promise(function(resolve, reject) { | ||
setTimeout(function() { | ||
timeout = setTimeout(function() { | ||
reject(new TimeoutError()); | ||
}, timeoutMillis); | ||
}) | ||
]); | ||
}), | ||
]).then(function(v) { | ||
clearTimeout(timeout); | ||
return v; | ||
}, function(err) { | ||
clearTimeout(timeout); | ||
throw err; | ||
}); | ||
}; |
{ | ||
"name": "promise-timeout", | ||
"version": "1.0.0", | ||
"version": "1.1.0", | ||
"description": "Simple timeouts for promises", | ||
@@ -9,8 +9,15 @@ "main": "index.js", | ||
}, | ||
"keywords": ["promise", "timeout"], | ||
"keywords": [ | ||
"promise", | ||
"timeout" | ||
], | ||
"author": "David M. Lee, II <leedm777@yahoo.com>", | ||
"license": "MIT", | ||
"repository": { | ||
"type": "git", | ||
"url": "https://github.com/building5/promise-timeout.git" | ||
}, | ||
"devDependencies": { | ||
"mocha": "^2.3.4" | ||
"mocha": "^3.2.0" | ||
} | ||
} |
@@ -26,5 +26,6 @@ // Copyright (c) 2015 David M. Lee, II | ||
describe('a fast promise', function() { | ||
it('should resolve', function() { | ||
return pt.timeout(later(10), 1000) | ||
.then(function() { | ||
it('should resolve with correct value', function() { | ||
return pt.timeout(Promise.resolve('some value'), 1000) | ||
.then(function(val) { | ||
assert.equal(val, 'some value'); | ||
}, function(err) { | ||
@@ -34,3 +35,11 @@ assert.fail('should have resolved'); | ||
}); | ||
it('should reject with correct exception', function() { | ||
return pt.timeout(Promise.reject(new Error('some error')), 1000) | ||
.then(function(val) { | ||
assert.fail('should have rejected'); | ||
}, function(err) { | ||
assert.equal(err.message, 'some error'); | ||
}); | ||
}); | ||
}); | ||
}); |
No repository
Supply chain riskPackage does not have a linked source code repository. Without this field, a package will have no reference to the location of the source code use to generate the package.
Found 1 instance in 1 package
5256
74
0