kozhevnikov / proxymise

Chainable Promise Proxy

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Documentation Suggestion

robblovell opened this issue · comments

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)