diff --git a/CHANGELOG.md b/CHANGELOG.md index 064b142..75a0dda 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -6,6 +6,11 @@ and this project adheres to [Semantic Versioning](http://semver.org/). For changes before version 0.4.0, please see the commit history +## [0.8.1] - 2020-11-28 + +### Updated +- Made the promise executor optional; + ## [0.8.0] - 2020-11-27 ### Added diff --git a/README.md b/README.md index ab80b73..2ddab4c 100644 --- a/README.md +++ b/README.md @@ -444,7 +444,7 @@ CPromise class **Extends**: Promise * [~CPromise](#module_CPromise..CPromise) ⇐ Promise - * [new CPromise(executor, [options])](#new_module_CPromise..CPromise_new) + * [new CPromise([executor], [options])](#new_module_CPromise..CPromise_new) * _instance_ * [.signal](#module_CPromise..CPromise+signal) : AbortSignal * [.isPending](#module_CPromise..CPromise+isPending) ⇒ Boolean @@ -454,7 +454,7 @@ CPromise class * [.parent](#module_CPromise..CPromise+parent) ⇒ CPromise \| null * [.totalWeight([weight])](#module_CPromise..CPromise+totalWeight) ⇒ Number \| CPromise * [.innerWeight([weight])](#module_CPromise..CPromise+innerWeight) ⇒ Number \| CPromise - * [.progress(value, [data])](#module_CPromise..CPromise+progress) ⇒ Number \| CPromise + * [.progress([value], [data])](#module_CPromise..CPromise+progress) ⇒ Number \| CPromise * [.propagate(type, data)](#module_CPromise..CPromise+propagate) ⇒ CPromise * [.captureProgress([options])](#module_CPromise..CPromise+captureProgress) ⇒ CPromise * [.scopes()](#module_CPromise..CPromise+scopes) ⇒ Array.<CPromise> @@ -489,13 +489,13 @@ CPromise class -#### new CPromise(executor, [options]) -Constructs new CPromise instance +#### new CPromise([executor], [options]) +Creates a new CPromise instance | Param | Type | Description | | --- | --- | --- | -| executor | CPromiseExecutorFn | promise executor function that will be invoked in the context of the new CPromise instance | +| [executor] | CPromiseExecutorFn | promise executor function that will be invoked in the context of the new CPromise instance | | [options] | CPromiseOptions | | @@ -558,14 +558,14 @@ Set or get the total weight of the inner chains -#### cPromise.progress(value, [data]) ⇒ Number \| CPromise +#### cPromise.progress([value], [data]) ⇒ Number \| CPromise Set promise progress **Kind**: instance method of [CPromise](#module_CPromise..CPromise) | Param | Type | Description | | --- | --- | --- | -| value | Number | a number between [0, 1] | +| [value] | Number | a number between [0, 1] | | [data] | \* | any data to send for progress event listeners | diff --git a/lib/c-promise.js b/lib/c-promise.js index e25285a..9f02bbe 100644 --- a/lib/c-promise.js +++ b/lib/c-promise.js @@ -153,8 +153,8 @@ function resolveGenerator(generatorFn) { class CPromise extends Promise { /** - * Constructs new CPromise instance - * @param {CPromiseExecutorFn} executor - promise executor function that will be invoked + * Creates a new CPromise instance + * @param {CPromiseExecutorFn} [executor] - promise executor function that will be invoked * in the context of the new CPromise instance * @param {CPromiseOptions} [options] */ @@ -213,14 +213,20 @@ class CPromise extends Promise { this.onResume = this.onResume.bind(this); this.onCapture = this.onCapture.bind(this); - try { - executor.call(this, value => { - this.resolve(value) - }, err => { - this.reject(err) - }, this); - } catch (err) { - this.reject(err); + if (executor != null) { + if (typeof executor !== 'function') { + throw TypeError('CPromise resolver null is not a function'); + } + + try { + executor.call(this, value => { + this.resolve(value) + }, err => { + this.reject(err) + }, this); + } catch (err) { + this.reject(err); + } } signal !== undefined && this.listen(signal); @@ -337,7 +343,7 @@ class CPromise extends Promise { /** * Set promise progress - * @param {Number} value a number between [0, 1] + * @param {Number} [value] a number between [0, 1] * @param {*} [data] any data to send for progress event listeners * @returns {Number|CPromise} */ @@ -844,8 +850,8 @@ class CPromise extends Promise { return value; }, onRejected && ((err) => { - if(err instanceof CanceledError){ - promise[_shadow].isCanceled= true; + if (err instanceof CanceledError) { + promise[_shadow].isCanceled = true; } return onRejected.call(promise, err, promise) }) @@ -1021,12 +1027,12 @@ class CPromise extends Promise { } }) - scope.on('signal', (type, data)=>{ + scope.on('signal', (type, data) => { const {length} = pending; - let result= false; + let result = false; for (let i = 0; i < length; i++) { - if(pending[i].emitSignal(type, data)){ - result= true; + if (pending[i].emitSignal(type, data)) { + result = true; } } return result; @@ -1172,12 +1178,12 @@ class CPromise extends Promise { } }) - scope.on('signal', (type, data)=>{ + scope.on('signal', (type, data) => { const {length} = thenables; - let result= false; + let result = false; for (let i = 0; i < length; i++) { - if(thenables[i].emitSignal(type, data)){ - result= true; + if (thenables[i].emitSignal(type, data)) { + result = true; } } return result; @@ -1468,7 +1474,7 @@ prototype.removeEventListener = prototype.off; const typeL = type.toLowerCase(); prototype['on' + type] = function (listener) { if (!this[_shadow].isPending) { - throw Error(`Unable to subscribe to ${typeL} event since promise has been already settled`); + throw Error(`Unable to subscribe to ${typeL} event since the promise has been already settled`); } return this.on(typeL, listener); } diff --git a/package.json b/package.json index 2ec6713..44794b2 100644 --- a/package.json +++ b/package.json @@ -1,7 +1,7 @@ { "name": "c-promise2", "version": "0.8.0", - "description": "Promise with cancellation, timeouts, progress capturing, suspending and user signals support", + "description": "Promise with cancellation, pause, timeouts, progress capturing and user signals / data flow support", "author": { "name": "Dmitriy Mozgovoy", "email": "robotshara@gmail.com",