Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

kew

Package Overview
Dependencies
Maintainers
5
Versions
25
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

kew - npm Package Compare versions

Comparing version 0.3.1 to 0.3.2

34

kew.js

@@ -207,2 +207,36 @@ /** @typedef {function(?, ?)} */

/**
* Return a new promise that behaves the same as the current promise except
* that it will be rejected if the current promise does not get fulfilled
* after a certain amount of time.
*
* @param {number} timeoutMs The timeout threshold in msec
* @param {string=} timeoutMsg error message
* @returns a new promise with timeout
*/
Promise.prototype.timeout = function (timeoutMs, timeoutMsg) {
var deferred = new Promise()
var isTimeout = false
var timeout = setTimeout(function() {
deferred.reject(new Error(timeoutMsg || 'Promise timeout after ' + timeoutMs + ' ms.'))
isTimeout = true
}, timeoutMs)
this.then(function (data) {
if (!isTimeout) {
clearTimeout(timeout)
deferred.resolve(data)
}
},
function (err) {
if (!isTimeout) {
clearTimeout(timeout)
deferred.reject(err)
}
})
return deferred.promise
}
/**
* Attempt to resolve this promise with the specified input

@@ -209,0 +243,0 @@ *

2

package.json
{
"name": "kew"
, "description": "a lightweight promise library for node"
, "version": "0.3.1"
, "version": "0.3.2"
, "homepage": "https://github.com/Obvious/kew"

@@ -6,0 +6,0 @@ , "authors": [

@@ -379,1 +379,34 @@ var Q = require('../kew')

}
exports.testTimeout = function(test) {
var promise = Q.delay(50).timeout(45, 'Timeout message')
promise.then(function () {
test.fail('The promise is supposed to be timeout')
})
.fail(function (e) {
test.equals('Timeout message', e.message, 'The error message should be the one passed into timeout()')
})
.fin(test.done)
}
exports.testNotTimeout = function(test) {
var promise = Q.delay(40, 'expected data').timeout(45, 'Timeout message')
promise.then(function (data) {
test.equals('expected data', data, 'The data should be the data from the original promise')
})
.fail(function (e) {
test.fail('The promise is supposed to be resolved before the timeout')
})
.fin(test.done)
}
exports.testNotTimeoutButReject = function(test) {
var promise = Q.delay(40).then(function() {throw new Error('Reject message')}).timeout(45, 'Timeout message')
promise.then(function (data) {
test.fail('The promise is supposed to be rejected')
})
.fail(function (e) {
test.equals('Reject message', e.message, 'The error message should be from the original promise')
})
.fin(test.done)
}
SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc