kozhevnikov / proxymise Goto Github PK
View Code? Open in Web Editor NEWChainable Promise Proxy
License: MIT License
Chainable Promise Proxy
License: MIT License
to use the library in browser you need to use:
<script>var module = {}</script>
it would be nice if there was UMD module definition.
proxymise is great, thank you!
Unfortunately I've just discovered that it's not working with Knex.js queries. Here's an example which demonstrates the problem.
// $ npm install knex sqlite3 proxymise
import Knex from 'knex'
import proxymise from 'proxymise';
async function main() {
// create Knex connection
const knex = Knex({
client: 'sqlite3',
useNullAsDefault: true,
connection: {
filename: ':memory:',
},
});
// create a users table
await knex.raw(
'CREATE TABLE users ( ' +
' id INTEGER PRIMARY KEY ASC, ' +
' name TEXT ' +
')'
);
// insert a row
await knex('users').insert({
name: 'kozhevnikov'
});
// knex example without proxymise()
const row = await knex('users').select('id', 'name').where({ name: 'kozhevnikov' }).first();
const name = row.name;
console.log('EXPECT name: ', name);
// works if knex method are inside proxymise()
try {
const prxm1 = proxymise(
knex('users')
.select('id', 'name')
.where({ name: 'kozhevnikov' })
.first()
);
const name1 = await prxm1.name;
console.log('PASSED name1:', name1);
}
catch(error) {
console.log('FAILED name1:', error);
}
// doesn't work when knex methods are outside proxymise()
try {
const prxm2 = proxymise(
knex('users')
);
const name2 = await prxm2
.select('id', 'name')
.where({ name: 'kozhevnikov' })
.first()
.name;
console.log('PASSED name2:', name2);
}
catch(error) {
console.log('FAILED name2:', error);
}
knex.destroy();
}
main();
Output:
EXPECT name: kozhevnikov
PASSED name1: kozhevnikov
FAILED name2: TypeError: Function.prototype.apply was called on undefined, which is a undefined and not a function
at .../[email protected]/node_modules/proxymise/index.js:27:53
at async main (file:///.../proxymise-knex-bug.js:53:19)
It is unclear how to define types for proxymissed promises.
Providing these guidelines would encourage broader pattern adoption.
Documentation suggestion:
It would be good to include a simple implementation that isn't already built somewhere as an example for people who want to write a fluent client to do something. The code for Fetch, AWS, and Selenium isn't easy to understand!
This would be a bit of a different use case. The client itself would leverage proxymise to implement fluent async methods without the developer using the client having to leverage proxymise. It's already built into the client.
Here's my example: (I realize the methods below aren't async, but they do return promises... It's just a dumb example of the syntax required to build a fluent client that uses proxymise to have async methods).
person.js:
import proxymise from "proxymise";
export class PersonBuilder {
constructor() {
this.person = {};
return proxymise(this)
}
async withName(name) {
this.person.name = name;
return this;
}
async withAge(age) {
this.person.age = age;
return this;
}
async build() {
return this.person;
}
}
app.js
import { PersonBuilder } from "./person.js"
const personBuilder = new PersonBuilder()
const person = await personBuilder
.withName("Daffodil")
.withAge(25)
.build();
console.log(person);
(call out to: https://dev.to/nas5w/using-the-fluent-interface-pattern-to-create-javascript-objects-2p1n)
How does using proxymise affect the debugging experience?
Does it obscure the stack traces?
Proxies are still pretty slooooow. It would be nice to see how much of performance we're sacrificing by using this approach. Please add some benchmarks
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.