@js-bits/xpromise
Advanced tools
Comparing version 0.0.1 to 0.0.2
47
index.js
@@ -11,15 +11,11 @@ import enumerate from '@js-bits/enumerate'; | ||
// https://stackoverflow.com/questions/6598945/detect-if-function-is-native-to-browser | ||
const isNativeFunction = func => | ||
typeof func === 'function' && /\{\s+\[native code\]/.test(Function.prototype.toString.call(func)); | ||
/** | ||
* @class | ||
* @extends {Promise} | ||
*/ | ||
class ExtendablePromise extends Promise { | ||
constructor(...args) { | ||
const lastArg = args[args.length - 1]; | ||
if (isNativeFunction(lastArg)) { | ||
// internal promise call | ||
// eslint-disable-next-line constructor-super | ||
return super(lastArg); | ||
} | ||
/** | ||
* @param {Function} executor | ||
*/ | ||
constructor(executor) { | ||
let resolve; | ||
@@ -33,4 +29,7 @@ let reject; | ||
const [executor] = args; | ||
this[ø.executor] = executor; | ||
if (typeof executor === 'function') { | ||
this[ø.executor] = executor; | ||
} else { | ||
throw new Error('Invalid executor type'); | ||
} | ||
} | ||
@@ -43,7 +42,18 @@ | ||
/** | ||
* @param {...any} args | ||
* @returns {Promise} | ||
*/ | ||
execute(...args) { | ||
this[ø.executor](this[ø.resolve], this[ø.reject], ...args); | ||
if (this[ø.executor]) { | ||
this[ø.executor](this[ø.resolve], this[ø.reject], ...args); | ||
this[ø.executor] = undefined; | ||
} | ||
return this; | ||
} | ||
/** | ||
* @param {any} result | ||
* @returns {Promise} | ||
*/ | ||
resolve(...args) { | ||
@@ -54,2 +64,6 @@ this[ø.resolve](...args); | ||
/** | ||
* @param {Error} reason | ||
* @returns {Promise} | ||
*/ | ||
reject(...args) { | ||
@@ -61,2 +75,5 @@ this[ø.reject](...args); | ||
// https://stackoverflow.com/a/60328122 | ||
Object.defineProperty(ExtendablePromise, Symbol.species, { get: () => Promise }); | ||
export default ExtendablePromise; |
@@ -29,2 +29,9 @@ import { jest } from '@jest/globals'; | ||
}); | ||
describe('when an invalid executor is passed', () => { | ||
test('should throw an error', () => { | ||
expect(() => { | ||
promise = new ExtendablePromise(); | ||
}).toThrow('Invalid executor type'); | ||
}); | ||
}); | ||
}); | ||
@@ -38,2 +45,10 @@ | ||
}); | ||
test('should execute once', () => { | ||
expect(executorFunc).not.toHaveBeenCalled(); | ||
promise.execute(); | ||
promise.execute(); | ||
promise.execute(); | ||
promise.execute(); | ||
expect(executorFunc).toHaveBeenCalledTimes(1); | ||
}); | ||
test('should return the promise', () => { | ||
@@ -40,0 +55,0 @@ expect(promise.execute()).toBe(promise); |
{ | ||
"name": "@js-bits/xpromise", | ||
"version": "0.0.1", | ||
"version": "0.0.2", | ||
"description": "Extendable Promise", | ||
@@ -5,0 +5,0 @@ "keywords": [ |
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
8152
205