Giter VIP home page Giter VIP logo

then-ref's Introduction

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

npm install then-ref

Usage

Import thenRef from then-ref and wrap your synchronous or asynchronous API.

Synchronous API

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

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.

then-ref's People

Contributors

kid-joker avatar

Stargazers

 avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.