An open API service indexing awesome lists of open source software.

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

Awesome Lists containing this project

README

          

# A+ compliant promise implementation


Promises/A+ logo

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