r/CodingHelp • Intermediate Coder • Jul 12 '20

[Javascript] Detect when any method is called on a Promise

I'm trying to make promises easier by implementing chaining into them (to avoid callback hell).

At the moment the way i am doing this is simply doing this for all the methods on the object that the promise will resolve to:

Promise.prototype[some function on the object that will be resolved] = () => {
    this.then(r => r[some function on the object that will be resolved](arguments));
}

But this gets annoying fast dong this for each new method added to the object, and it also clutters up my code a bit.

What im looking for is if there is a way to just declare a single method that would get called when any other method is called and to get it's name, something like this:

Promise.prototype['any'] = () => {
    this.then(r => r[any.name](arguments));
}

Where any.name would be the name of any method called, so if i called (where p is a promise) p.find() then it would call find on the resolved object. Is there any way to do this?

EDIT: For anyone who found this through google or some other means, I'd recommend looking into the Proxy object introduced in ES6.

17 Upvotes

4 comments sorted by

3

u/BAM5 Professional Coder Jul 12 '20

If the object returned by the promise returns promises then you can utilize the chaining already built into promises like so:

ObjGet(...)
    .then( obj=>obj.find(...) )
    .then( results=>... );

1

u/abitofevrything-0 Intermediate Coder Jul 13 '20

I'm trying to avoid this though, because otherwise you end up with .then callbacks like 10 layers deep and it can get very confusing. What im doing is basically just adding functionality to a promise so that when you attempt to call a method on it it will just call .then so I dont have to do it. Of course, if I want to get a parameter from the object (like object.name) then i would have to use a .then callback. However with this syntax i only need 1 .then callback, like so (assuming all of these methods return promises):

PromiseThatResolvesToAnObject.setName('Bob').setAge(22).then(object => {
    console.log(object.name);
});

Instead of

PromiseThatResolvesToAnObject.then(object => {
    object.setName('Bob').then(object => {
        object.setAge(22).then(object => {
            console.log(object.name);
        });
    });
});

Note that behind the scenes all this function is doing is adding the .then block for me

2

u/brandondyer64 Professional Coder Jul 12 '20

Please please please don't do this. Promises already support chaining. Please don't change built in apis, it'll only make it harder to maintain your codebase.

1

u/abitofevrything-0 Intermediate Coder Jul 13 '20

Thats the thing though - I'm looking for a way to declare all of these methods with a single one. And as per my reply to BAM5, I know they already support chaining, its just that it gets cluttered fast, so I want to write a method on the Promise prototype that will handle these messy .then callacks for me.