https://github.com/js-bits/xpromise
Extendable Promise
https://github.com/js-bits/xpromise
Last synced: about 1 year ago
JSON representation
Extendable Promise
- Host: GitHub
- URL: https://github.com/js-bits/xpromise
- Owner: js-bits
- Created: 2021-04-25T02:14:31.000Z (about 5 years ago)
- Default Branch: master
- Last Pushed: 2023-07-19T02:16:44.000Z (almost 3 years ago)
- Last Synced: 2025-04-08T20:05:40.949Z (about 1 year ago)
- Language: JavaScript
- Size: 1.18 MB
- Stars: 3
- Watchers: 1
- Forks: 0
- Open Issues: 3
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# Extendable Promise
Allows extension of JavaScript's standard, built-in `Promise` class and decouples an asynchronous operation that ties an outcome to a promise from the constructor.
## Installation
Install with npm:
```
npm install @js-bits/xpromise
```
Install with yarn:
```
yarn add @js-bits/xpromise
```
Import where you need it:
```javascript
import ExtendablePromise from '@js-bits/xpromise';
```
or require for CommonJS:
```javascript
const ExtendablePromise = require('@js-bits/xpromise');
```
## How to use
```javascript
class MyPromise extends ExtendablePromise {
// do whatever you need
}
const myPromise = new MyPromise((resolve, reject) => {
console.log('executed', resolve, reject);
});
console.log(myPromise instanceof Promise); // true
myPromise.execute(); // 'executed' [Function (anonymous)] [Function (anonymous)]
myPromise.then(result => {
console.log(result); // 123
});
myPromise.resolve(123);
```
## Notes
- [Alternative solution](https://stackoverflow.com/questions/48158730/extend-javascript-promise-and-resolve-or-reject-it-inside-constructor)