https://github.com/jonaskuske/promise-aplus
🔮 An A+ compliant Promise implementation
https://github.com/jonaskuske/promise-aplus
Last synced: 11 months ago
JSON representation
🔮 An A+ compliant Promise implementation
- Host: GitHub
- URL: https://github.com/jonaskuske/promise-aplus
- Owner: jonaskuske
- License: mit
- Created: 2022-11-23T23:38:09.000Z (over 3 years ago)
- Default Branch: main
- Last Pushed: 2022-11-25T02:14:08.000Z (over 3 years ago)
- Last Synced: 2025-03-29T23:17:53.759Z (about 1 year ago)
- Language: JavaScript
- Size: 1.13 MB
- Stars: 2
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# A+ compliant promise implementation
A Promise implementation that utilizes modern ES2022 features like private class fields and fully complies with the [Promises/A+ 1.1.0 specification](https://promisesaplus.com/). Requires a runtime with `queueMicrotask` like browsers or Node.
### Available functions
```js
// Create new Promise that immediately resolves
promise = Promise.resolve(valueOrThenable)
// Create new Promise that immediately rejects
promise = Promise.reject(reason)
// Create new Promise, must pass executor functiom
promise = new Promise((resolve, reject) => {})
// Return new Promises with resolve/reject handlers applied
promise.then(onResolve, onReject)
promise.catch(onReject)
```
### Example
```js
import { Promise } from "./promise.js"
function retryIfEmpty(fn, retry = 1, signal) {
retry = Math.max(0, retry)
const end = new (class extends Error {
name = "RetryError"
message = "maximum tries reached"
})()
return new Promise((resolve, reject) => {
signal?.throwIfAborted()
signal?.addEventListener("abort", reject)
Promise.resolve(fn()).then((val) => {
resolve(
val ?? (retry > 0 ? retryIfEmpty(fn, --retry, signal) : Promise.reject(end)),
)
}, reject)
})
}
retryIfEmpty(() => Math.random() > 0.5 || null, 2).catch((e) => {
if (e.name !== "RetryError") throw e
return false
})
```
### Verify
**`yarn test`**: Run A+ test suite against this Promise implementation
**`yarn test-builtin`**: Run A+ test suite against the native built-in `Promise`
**`yarn test-deferred`**: Run deferred tests and A+ test suite against `DeferredPromise`
---
©️ 2022, Jonas Kuske