Comparing version 0.1.7 to 0.1.8
@@ -60,9 +60,6 @@ /** | ||
var timeout = options.timeout !== undefined ? options.timeout : this._timeout; | ||
var promise = this._createPromise(id, fn); | ||
if (timeout) { | ||
var timeoutPromise = this._createTimeoutPromise(id, timeout); | ||
return Promise.race([promise, timeoutPromise]); | ||
} else { | ||
return promise; | ||
} | ||
var mainPromise = this._createPromise(id, fn); | ||
var finalPromise = timeout ? this._wrapWithTimeout(mainPromise, id, timeout) : mainPromise; | ||
this._setPromise(id, finalPromise); | ||
return finalPromise; | ||
} | ||
@@ -158,3 +155,3 @@ | ||
value: function getPromise(id) { | ||
return this._map[id] && this._map[id].promise; | ||
return this._map[id] && this._map[id].finalPromise; | ||
} | ||
@@ -212,2 +209,15 @@ | ||
} | ||
}, { | ||
key: '_wrapWithTimeout', | ||
value: function _wrapWithTimeout(promise, id, timeout) { | ||
var timeoutPromise = this._createTimeoutPromise(id, timeout); | ||
return Promise.race([promise, timeoutPromise]); | ||
} | ||
}, { | ||
key: '_setPromise', | ||
value: function _setPromise(id, promise) { | ||
if (this._map[id]) { | ||
this._map[id].finalPromise = promise; | ||
} | ||
} | ||
}]); | ||
@@ -214,0 +224,0 @@ |
{ | ||
"name": "pendings", | ||
"version": "0.1.7", | ||
"version": "0.1.8", | ||
"description": "Better control of pending promises", | ||
@@ -5,0 +5,0 @@ "author": { |
@@ -47,9 +47,6 @@ /** | ||
const timeout = options.timeout !== undefined ? options.timeout : this._timeout; | ||
const promise = this._createPromise(id, fn); | ||
if (timeout) { | ||
const timeoutPromise = this._createTimeoutPromise(id, timeout); | ||
return Promise.race([promise, timeoutPromise]); | ||
} else { | ||
return promise; | ||
} | ||
const mainPromise = this._createPromise(id, fn); | ||
const finalPromise = timeout ? this._wrapWithTimeout(mainPromise, id, timeout) : mainPromise; | ||
this._setPromise(id, finalPromise); | ||
return finalPromise; | ||
} | ||
@@ -123,3 +120,3 @@ | ||
getPromise(id) { | ||
return this._map[id] && this._map[id].promise; | ||
return this._map[id] && this._map[id].finalPromise; | ||
} | ||
@@ -166,2 +163,13 @@ | ||
} | ||
_wrapWithTimeout(promise, id, timeout) { | ||
const timeoutPromise = this._createTimeoutPromise(id, timeout); | ||
return Promise.race([promise, timeoutPromise]); | ||
} | ||
_setPromise(id, promise) { | ||
if (this._map[id]) { | ||
this._map[id].finalPromise = promise; | ||
} | ||
} | ||
} | ||
@@ -168,0 +176,0 @@ |
@@ -187,2 +187,8 @@ 'use strict'; | ||
it('should return promise of timeouted pending', function () { | ||
const p = this.pendings.set(1, () => {}, {timeout: 10}); | ||
assert.equal(p, this.pendings.getPromise(1)); | ||
return assert.isRejected(p); | ||
}); | ||
it('should return undefined for non-existing pending', function () { | ||
@@ -189,0 +195,0 @@ assert.equal(this.pendings.getPromise(1)); |
30442
790