https://github.com/webreflection/esimorp
An Inside Out Promise
https://github.com/webreflection/esimorp
cancelable cancellation promises rejectable resolvable
Last synced: about 1 month ago
JSON representation
An Inside Out Promise
- Host: GitHub
- URL: https://github.com/webreflection/esimorp
- Owner: WebReflection
- License: isc
- Created: 2017-03-01T19:03:20.000Z (over 8 years ago)
- Default Branch: master
- Last Pushed: 2018-07-03T18:11:24.000Z (over 7 years ago)
- Last Synced: 2025-02-16T18:15:53.385Z (8 months ago)
- Topics: cancelable, cancellation, promises, rejectable, resolvable
- Language: HTML
- Size: 22.5 KB
- Stars: 7
- Watchers: 3
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# Esimorp [](https://travis-ci.org/WebReflection/esimorp) [](https://coveralls.io/github/WebReflection/esimorp?branch=master)
Inside Out Promises.
The simplest, yet handy, resolvable and rejectable Promises for all developers use cases.
```js
// The constructor callback is optional.
// If provided, it's exactly the same
// that you would pass to a new Promise.
const promise = new Esimorp();// you can already use the instance
// just like any other Promise
promise
.then(console.log)
.catch(console.error);// ... and whenever it happens ...
promise.resolve('kudos');
```### F.A.Q.
* _can I `promise.then().resolve()` ?_ Nope, only the initial resolvable creator/owner can resolve it. No side effects at distance.
* _can I safely pass around a `promise.then()` ?_ Absolutely, that just creates a new `Promise` copy that's not resolvable.
* _how can I abort an operation ?_ It's up to you, but following there is an example.```js
function fetchy(url) {const p = new Esimorp();
const xhr = new XMLHttpRequest();// derive from Esimorp
// to avoid external resolution
const out = p.then();
// expose only abort, delegating to xhr
out.abort = xhr.abort.bind(xhr);// resolve indirectly out through Esimorp p
xhr.addEventListener('abort', e => p.reject(e));
xhr.addEventListener('error', e => p.reject(e));
xhr.addEventListener('load', e => p.resolve(xhr));// perform the operation
xhr.open('get', url);
xhr.send();// return the Promise with abort delegate
return out;
}
```* _what else could I do with this ?_ You could create Promises with a timeout and resolve or reject them before.
```js
const p = new Esimorp((res, rej) => {
// reject in 5 seconds
setTimeout(rej, 5000, 'timeout');
});// but if resolved or rejected first is OK
setTimeout(() => p.resolve('OK'), 1000);
```### License
(C) 2017 Andrea Giammarchi, [@WebReflection](https://twitter.com/WebReflection/), MIT Style License.