Skip to content

Commit

Permalink
Added signals support;
Browse files Browse the repository at this point in the history
Added pause/resume methods;
Added allSettled method;
Removed CPromiseScope;
Refactored;
  • Loading branch information
DigitalBrainJS committed Nov 22, 2020
1 parent b4e7340 commit ee002ea
Show file tree
Hide file tree
Showing 11 changed files with 2,362 additions and 2,995 deletions.
493 changes: 246 additions & 247 deletions README.md

Large diffs are not rendered by default.

13 changes: 11 additions & 2 deletions jsdoc2md/README.hbs.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,14 @@
![David](https://img.shields.io/david/DigitalBrainJS/c-promise)

## SYNOPSIS :sparkles:
A Promise class built on top of the native that supports some additional features such as cancellation, timeouts, progress capturing and concurrency limit.

This library provides an advanced version of the built-in Promise by subclassing.
You might be interested in using it if you need the following features:
- promise cancellation (including nested)
- progress capturing
- promise suspending
- timeouts
- concurrent limit for `all` and `allSettled` methods with `mapper` reducer

In terms of the library **the cancellation means rejection with a special error subclass**.

Expand Down Expand Up @@ -79,6 +86,8 @@ has lost its relevance to you.
- `CPromise.all` supports concurrency limit
- `CPromise.all` and `CPromise.race` methods have cancellation support, so the others nested pending promises will be canceled
when the result promise settled
- promise suspending (using `pause` and `resume` methods)
- custom signals (`emitSignal`)
- `delay` method to return promise that will be resolved with the value after timeout
- ability to set the `weight` for each promise in the chain to manage the impact on chain progress
- ability to attach meta info on each setting of the progress
Expand Down Expand Up @@ -222,7 +231,7 @@ const promise= CPromise.from(function*(){
yield [[CPromise.delay(1000), CPromise.delay(1500)]] // resolve chains using CPromise.race([...chains]);
yield new CPromise(resolve=> resolve(true)); // any thenable object will be resolved
return "It works!";
}, [1, 2, 3])
})
.progress(value=> console.log(`Progress: ${value}`))
.then(message=> console.log(`Done: ${message}`));
````
Expand Down
23 changes: 20 additions & 3 deletions lib/abort-controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ class AbortSignal{
* AbortController class
*/

class AbortController {
class AbortControllerPolyfill {
/**
* Constructs new AbortController instance
*/
Expand Down Expand Up @@ -129,7 +129,24 @@ class AbortController {
}
}

const isAbortSignal = (thing) => {
return thing &&
typeof thing === 'object' &&
typeof thing.aborted === 'boolean' &&
typeof thing.addEventListener === 'function' &&
typeof thing.removeEventListener === 'function';
}

module.exports= {
AbortController,
AbortSignal
AbortController: (() => {
try {
if (typeof AbortController === 'function' && (new AbortController()).toString() === '[object AbortController]') {
return AbortController;
}
} catch (e) {
}
return AbortControllerPolyfill;
})(),
AbortSignal,
isAbortSignal
};

0 comments on commit ee002ea

Please sign in to comment.