mongoosejs / kareem

Next-generation take on pre/post function hooks
Apache License 2.0
82 stars 8 forks source link

hooks-fixed #9

Closed alexandruluca closed 7 years ago

alexandruluca commented 7 years ago

Hi,

Since I can't open an issue in hooks-fixed (repo forked by you), I chose yo put it here, hope it doesn't bother you.

I have this small example

var hooks = require('hooks-fixed');

var o = {
    install: function() {
        return new Promise((resolve, reject) => {
            return o.update().then(r => console.log(r));
        });
    },
    update: function() {
        return Promise.resolve("test");
    }
};

for(var k in hooks) {
    o[k] = hooks[k];
}

o.pre('install', function(next) {
    console.log(`preinstall`);
    return next();
});
o.pre('update', function(next) {
    console.log(`preupdate`);
    return next();
});

console.log(o.install());

Basically, when calling a function which has a hook from a function that also has a hook, the result of the function which will get called in the first hook is undefined (in this case, the function is o.update, which is called from o.install)

This is the error I am getting, it reffers to


node:17292) UnhandledPromiseRejectionWarning: Unhandled promise rejection (rejection id: 1): TypeError: Cannot read property 'then' of undefined o.update() from o.install() being undefined
```javascript

Is this normal behaviour or is there a workaround for this?
vkarpov15 commented 7 years ago

Unfortunately not, hooks-fixed and kareem are both meant to work with callbacks, not promises. If you pass a function that returns a promise through hooks, it won't return a promise anymore, need to use callbacks.