https://github.com/posva/faked-promise
Create a fake promise that can be resolve and rejected programatically
https://github.com/posva/faked-promise
mock promise testing
Last synced: 9 months ago
JSON representation
Create a fake promise that can be resolve and rejected programatically
- Host: GitHub
- URL: https://github.com/posva/faked-promise
- Owner: posva
- License: mit
- Created: 2018-01-28T17:13:06.000Z (over 8 years ago)
- Default Branch: master
- Last Pushed: 2024-12-13T21:36:08.000Z (over 1 year ago)
- Last Synced: 2024-12-28T10:02:32.523Z (over 1 year ago)
- Topics: mock, promise, testing
- Language: JavaScript
- Size: 662 KB
- Stars: 12
- Watchers: 3
- Forks: 0
- Open Issues: 2
-
Metadata Files:
- Readme: README.md
- Contributing: .github/CONTRIBUTING.md
- Funding: .github/funding.yml
- License: LICENSE
- Code of conduct: .github/CODE_OF_CONDUCT.md
Awesome Lists containing this project
README
# faked-promise [](https://circleci.com/gh/posva/faked-promise) [](https://www.npmjs.com/package/faked-promise) [](https://codecov.io/github/posva/faked-promise) [](https://github.com/posva/thanks)
> Create a promise that can be resolved and rejected programatically outside of it
It basically extracts the `resolve` and `reject` callbacks so you can call them whenever you need.
⚠️ The promise can only be resolved once, it's still a regular Promise after all.
> [!TIP]
> In modern environments, you can directly use [`Promise.withResolvers()`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise/withResolvers)
## Installation
```sh
npm i faked-promise
```
## Usage
faked-promise exports one single function that takes no arguments and returns an array with 3 elements:
- A real promise
- Its `resolve` callback
- Its `reject` callback
Meaning you can control exactly when the promise is resolved or rejected:
```js
const fakePromise = require('faked-promise')
const [promise, resolve, reject] = fakePromise()
promise.then(() => {})
resolve('any value')
```
## License
[MIT](http://opensource.org/licenses/MIT)