https://github.com/kid-joker/then-ref
then-ref is a utility library designed to standardize the APIs of third-party libraries, providing consistent then, catch, and finally methods for both synchronous and asynchronous APIs.
https://github.com/kid-joker/then-ref
asynchronous promise-api proxy synchronous thenable
Last synced: 5 months ago
JSON representation
then-ref is a utility library designed to standardize the APIs of third-party libraries, providing consistent then, catch, and finally methods for both synchronous and asynchronous APIs.
- Host: GitHub
- URL: https://github.com/kid-joker/then-ref
- Owner: KID-joker
- License: mit
- Created: 2024-07-07T00:51:24.000Z (over 1 year ago)
- Default Branch: main
- Last Pushed: 2024-07-14T00:03:36.000Z (over 1 year ago)
- Last Synced: 2025-05-15T16:18:41.693Z (5 months ago)
- Topics: asynchronous, promise-api, proxy, synchronous, thenable
- Language: TypeScript
- Homepage:
- Size: 62.5 KB
- Stars: 1
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# then-ref
`then-ref` is a utility library designed to standardize the APIs of third-party libraries. It provides a consistent interface for handling both synchronous and asynchronous operations using the familiar then, catch, and finally methods.
## Features
- Consistent API: Use the same interface for both synchronous and asynchronous APIs.
- Proxy Handling: Access the original value and remove the proxy using the value property.## Installation
```bash
npm install then-ref
```## Usage
Import `thenRef` from `then-ref` and wrap your synchronous or asynchronous API.
### Synchronous API
```js
import thenRef from 'then-ref'function syncApi() {
// Your synchronous function here
}thenRef(syncApi)()
.then((res) => {
console.log('Result:', res)
})
.catch((err) => {
console.error('Error:', err)
})
.finally(() => {
console.log('Operation complete.')
})// Access the original value and remove the proxy
const originalValue = thenRef(syncApi)().value
console.log('Original Value:', originalValue)
```### Asynchronous API
```js
import thenRef from 'then-ref'async function asyncApi() {
// Your asynchronous function here
}thenRef(asyncApi)()
.then((res) => {
console.log('Result:', res)
})
.catch((err) => {
console.error('Error:', err)
})
.finally(() => {
console.log('Operation complete.')
})// Access the original value and remove the proxy
const originalAsyncValue = await thenRef(asyncApi)().value
console.log('Original Value:', originalAsyncValue)
```## How It Works
`then-ref` wraps your API (synchronous or asynchronous) and returns a proxy object. This proxy object supports then, catch, and finally methods to handle the API's result consistently, regardless of its nature (sync or async).
The value property allows you to access the original value and remove the proxy. For asynchronous APIs, the value property still returns a promise, so you need to await it to get the final result.