Promiseproxy
Promisify callback-style APIs with ES2015 Proxy :whale:
Install / Use
/learn @slikts/PromiseproxyREADME
Lightweight promisified API wrappers
A yet another library for promisifying callback-style APIs, but this time implemented using the ES2015 Proxy object. It works by intercepting method calls to the API and returning a promise if a callback parameter was expected.
The benefit of using proxies is that the API is extended without the need to duplicate or mutate the original API implementation. The main functionality of the proxies is implemented in less than 20 lines, making this approach lightweight and easily auditable.
Used in
- promiseproxy-chrome – Promisified Chrome extension API
- promiseproxy-node – Promisified Node.js 6.x API
About Proxy
Requirements
Proxy requires native ES2015 support since it's not practicable to shim it for ES5 environments. It is supported in Node.js 6+, Chrome, Firefox and Edge.
API
Example
const {PromiseProxy} = require("promiseproxy")
<a name="exp_module_promiseproxy--PromiseProxy"></a>
PromiseProxy(target, schema) ⇒ <code>Proxy</code> ⏏
Factory of Proxy objects for recursively promisifying a callback-based API
Kind: global method of <code>promiseproxy</code>
| Param | Type | Description | | --- | --- | --- | | target | <code>Object</code> | The API to be promisifed | | schema | <code>Object</code> | API structure with callback parameter position |
Example
// Define chrome.tabs.query(_, callback) and .update(_, _, callback) methods
// 1 and 2 are the positions of the callback parameters (zero-based)
const schema = {tabs: {query: 1, update: 2}}
// Promisify the Chrome API based on the schema
const _chrome = PromiseProxy(chrome, schema)
// The promisified methods return a Promise if the callback parameter is omitted
_chrome.tabs.query(info).then(callback)
// The same methods can still be used with a callback
_chrome.tabs.query(info, callback)
Related Skills
node-connect
340.5kDiagnose OpenClaw node connection and pairing failures for Android, iOS, and macOS companion apps
frontend-design
84.2kCreate distinctive, production-grade frontend interfaces with high design quality. Use this skill when the user asks to build web components, pages, or applications. Generates creative, polished code that avoids generic AI aesthetics.
openai-whisper-api
340.5kTranscribe audio via OpenAI Audio Transcriptions API (Whisper).
commit-push-pr
84.2kCommit, push, and open a PR
