Skip to content

Commit

Permalink
Made the promise executor optional;
Browse files Browse the repository at this point in the history
  • Loading branch information
DigitalBrainJS committed Nov 28, 2020
1 parent 3d87a7f commit 50e6649
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 30 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
14 changes: 7 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -444,7 +444,7 @@ CPromise class
**Extends**: <code>Promise</code>

* [~CPromise](#module_CPromise..CPromise) ⇐ <code>Promise</code>
* [new CPromise(executor, [options])](#new_module_CPromise..CPromise_new)
* [new CPromise([executor], [options])](#new_module_CPromise..CPromise_new)
* _instance_
* [.signal](#module_CPromise..CPromise+signal) : <code>AbortSignal</code>
* [.isPending](#module_CPromise..CPromise+isPending) ⇒ <code>Boolean</code>
Expand All @@ -454,7 +454,7 @@ CPromise class
* [.parent](#module_CPromise..CPromise+parent) ⇒ <code>CPromise</code> \| <code>null</code>
* [.totalWeight([weight])](#module_CPromise..CPromise+totalWeight) ⇒ <code>Number</code> \| <code>CPromise</code>
* [.innerWeight([weight])](#module_CPromise..CPromise+innerWeight) ⇒ <code>Number</code> \| <code>CPromise</code>
* [.progress(value, [data])](#module_CPromise..CPromise+progress) ⇒ <code>Number</code> \| <code>CPromise</code>
* [.progress([value], [data])](#module_CPromise..CPromise+progress) ⇒ <code>Number</code> \| <code>CPromise</code>
* [.propagate(type, data)](#module_CPromise..CPromise+propagate) ⇒ <code>CPromise</code>
* [.captureProgress([options])](#module_CPromise..CPromise+captureProgress) ⇒ <code>CPromise</code>
* [.scopes()](#module_CPromise..CPromise+scopes) ⇒ <code>Array.&lt;CPromise&gt;</code>
Expand Down Expand Up @@ -489,13 +489,13 @@ CPromise class

<a name="new_module_CPromise..CPromise_new"></a>

#### new CPromise(executor, [options])
Constructs new CPromise instance
#### new CPromise([executor], [options])
Creates a new CPromise instance


| Param | Type | Description |
| --- | --- | --- |
| executor | <code>CPromiseExecutorFn</code> | promise executor function that will be invoked in the context of the new CPromise instance |
| [executor] | <code>CPromiseExecutorFn</code> | promise executor function that will be invoked in the context of the new CPromise instance |
| [options] | <code>CPromiseOptions</code> | |

<a name="module_CPromise..CPromise+signal"></a>
Expand Down Expand Up @@ -558,14 +558,14 @@ Set or get the total weight of the inner chains

<a name="module_CPromise..CPromise+progress"></a>

#### cPromise.progress(value, [data]) ⇒ <code>Number</code> \| <code>CPromise</code>
#### cPromise.progress([value], [data]) ⇒ <code>Number</code> \| <code>CPromise</code>
Set promise progress

**Kind**: instance method of [<code>CPromise</code>](#module_CPromise..CPromise)

| Param | Type | Description |
| --- | --- | --- |
| value | <code>Number</code> | a number between [0, 1] |
| [value] | <code>Number</code> | a number between [0, 1] |
| [data] | <code>\*</code> | any data to send for progress event listeners |

<a name="module_CPromise..CPromise+propagate"></a>
Expand Down
50 changes: 28 additions & 22 deletions lib/c-promise.js
Original file line number Diff line number Diff line change
Expand Up @@ -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]
*/
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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}
*/
Expand Down Expand Up @@ -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)
})
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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);
}
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -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",
Expand Down

0 comments on commit 50e6649

Please sign in to comment.