async-proxy

Create asynchronous nesting proxy in Node.js and browser.

Usage no npm install needed!

<script type="module">
  import asyncProxy from 'https://cdn.skypack.dev/async-proxy';
</script>

README

async-proxy npm GitHub license Build Status Coverage Status

Create asynchronous nesting proxy in Node.js and browser.

Installation

npm install --save async-proxy
# or
yarn add async-proxy
import createAsyncProxy from 'async-proxy'

Usage

Just modify handlers to suit your project.

Default handler(get, set, remove methods come from object-path-operator module):

{
  async get(target, path) {
    return get(target, path)
  }
, async apply(target, path, caller, args) {
    return Reflect.apply(get(target, path), caller, args)
  }
, async set(target, path, value) {
    set(target, path, value) // The return value will be ignored
  }
, async deleteProperty(target, path) {
    remove(target, path) // The return value will be ignored
  }
, async construct(target, path, args) {
    return Reflect.construct(get(target, path), args)
  }
, async setPrototypeOf(target, path, prototype) {
    return Reflect.setPrototypeOf(get(target, path), prototype) // The return value will be ignored
  }
, async getPrototypeOf(target, path) {
    return Reflect.getPrototypeOf(get(target, path))
  }
, async defineProperty(target, path, prop, descriptor) {
    return Reflect.defineProperty(get(target, path), prop, descriptor) // The return value will be ignored
  }
}

Example

create

const localBaseObj = {}

const proxy = createAsyncProxy(localBaseObj, {})

get

;(async () => {
  await proxy.something.asynchronous.property
})()

apply

;(async () => {
  // handled by apply handler
  await proxy.something.asynchronous.method(some, args)

  // handled by get handler
  ;(await proxy.something.asynchronous.method)(some, args)
})()

set

;(async () => {
  // The return value is not reliable, ou should check the results by other means to ensure that the operation is successful.
  // Non-blocking when it is an asynchronous operation, so your asynchronous operation should have an inside sequence queue.
  proxy.something.asynchronous.property = 'something'
})()

delete

;(async () => {
  // The return value is not reliable, ou should check the results by other means to ensure that the operation is successful.
  // Non-blocking when it is an asynchronous operation, so your asynchronous operation should have an inside sequence queue.
  delete proxy.something.asynchronous.property
})()

new

;(async () => {
  // handled by construct handler
  await new proxy.something.asynchronous.construct()

  // handled by get handler
  new (await proxy.something.construct)()
})()

for await

;(async () => {
  for await (const prop of await proxy.something.enumerable) {
    ...
  }
})()

setPrototypeOf

;(async () => {
  // The return value is not reliable, ou should check the results by other means to ensure that the operation is successful.
  // Non-blocking when it is an asynchronous operation, so your asynchronous operation should have an inside sequence queue.
  Object.setPrototypeOf(proxy.something.asynchronous.something, Array.prototype)
})

getPrototypeOf

;(async () => {
  // handled by getPrototypeOf handler
  await Object.getPrototypeOf(proxy.something.asynchronous.property)

  // handled by get handler
  Object.getPrototypeOf(await proxy.something.asynchronous.property)
})()

defineProperty

;(async () => {
  // The return value is not reliable, ou should check the results by other means to ensure that the operation is successful.
  // Non-blocking when it is an asynchronous operation, so your asynchronous operation should have an inside sequence queue.
  Object.defineProperty(proxy.something.asynchronous, 'property', {
    value: 'something'
  })
})()

API

Table of Contents

createAsyncProxy

Create a Proxy to asynchronous operate object.

Parameters

Returns Proxy Async Proxy