https://github.com/conversejs/openpromise
An open promise is a promise that can be resolved or rejected outside of its closure
https://github.com/conversejs/openpromise
Last synced: 2 months ago
JSON representation
An open promise is a promise that can be resolved or rejected outside of its closure
- Host: GitHub
- URL: https://github.com/conversejs/openpromise
- Owner: conversejs
- Created: 2021-04-27T20:36:58.000Z (about 5 years ago)
- Default Branch: master
- Last Pushed: 2021-04-27T21:15:06.000Z (about 5 years ago)
- Last Synced: 2024-04-26T10:22:30.853Z (about 2 years ago)
- Language: JavaScript
- Size: 11.7 KB
- Stars: 0
- Watchers: 1
- Forks: 2
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# openpromise
JavaScript promises require that you call `reject` or `resolve` inside the closure of the created promise.
For example:
```
const promise = new Promise((resolve, reject) => {
// We have to call resolve or reject here, inside the closure
});
// We can't resolve the promise here, outside of the closure
```
This package lets you create an *open promise*. This is a normal JavaScript
Promise, but with its `resolve` and `reject` callbacks set as attributes on the promise itself.
This lets you resolve or reject the promise outside of its closure.
For example:
```
import { getOpenPromise } from 'openpromise';
const promise = getOpenPromise();
const func = setTimeout(() => promise.resolve(), 1000);
return promise;
```
## Promise.withResolvers
This package predates the newer built-in [Promise.withResolvers](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise/withResolvers) static method.
However, it's been updated to make use of `Promise.withResolvers` in case it's available.