luciotato / waitfor

Sequential programming for node.js, end of callback hell / pyramid of doom
MIT License
531 stars 29 forks source link

Static fix #6

Closed jnugh closed 11 years ago

jnugh commented 11 years ago

The this context matters even outside of objects, for example when you use waitfor with mongoose, where methods are kind of static.

Example: Mongoose uses the this context in Model.find which is being called without having an instance of Model, however the this namespace referes to different methods which belong to Model.

wait.forMethod checks if obj is an object, in this case waitfor needs to allow function to be passed as well.

luciotato commented 11 years ago

In order to understand the use case, can you show me the code where you're using wait.for, and you're calling a method of a function?

jnugh commented 11 years ago

Sure thing, with my patch the following works (userModel is a mongoose Model):

var blub = wait.forMethod(userModel, 'find', {
'_id' : mongoose.Types.ObjectId(req.params.id)
});

The following code however:

var blub = wait.for(userModel.find, {
'_id' : mongoose.Types.ObjectId(req.params.id)
});

results in:

TypeError: Object # has no method 'applyNamedScope' at find (**/nodemodules/mongoose/lib/model.js:816:8) at Object.Wait.applyAndWait (**/node_modules/wait.for/waitfor.js:41:12) at Object.Wait [as for] (***/node_modules/wait.for/waitfor.js:56:21)

THe type of userModel is function obviously.

jnugh commented 11 years ago

I created a simple example: https://gist.github.com/gh1234/6338683 Wait.for makes no sense here actually (no callbacks etc.) But the error occures nevertheless.

luciotato commented 11 years ago

ok, thanks! I've removed the "if" completely. First arg could be even an Array of async fns, and it would make sense also (example: a jmp table) Anyway, most programming errors will be catched by the 2nd if.