-
Notifications
You must be signed in to change notification settings - Fork 1.2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Support promises #418
Comments
+1 |
2 similar comments
+1 |
+1 |
+1
|
This might be of interest to folks - talk on InfoQ about large nodejs project at kixeye where Paul Hill discusses why they liked Promises - http://www.infoq.com/presentations/kixeye-rest-api. While there is a ton of useful info in this talk, he specifically discusses Promises (they used Bluebird) starting at about 21:50. Besides the syntactic sugar/readability, he highlights how there is an "implicit try-catch" that allows centralizing error handling/logging. He also shows a code snippet (25:51) for how they did this that might be a useful pattern to consider including in the docs at some point, if possible. |
Thank you all for the input. We're building a list of roadmap items at https://github.com/strongloop/loopback/wiki/Roadmap-brainstorming. 'Promise' is on the list too. Maybe we should move it to a gist so that community ideas can be accepted via PRs. |
I've actually been using something similar...after
...pardon my coffeescript :) This will promisify all the app's models at once. I have a couple more items in my filter to exclude attributes we don't need to promisify, but it basically works the same. The main problem is that you can't use the promises in a boot script or model init script, but I think @coodoo takes care of that with his solution above (thanks!). You can use @coodoo's method for complex models, and then use the app.models loop to take care of the rest (it's ok to run The other problem I've found is that it doesn't work for the model relations. For example:
will fail...I'm not sure how to go about promisifying those "virtual selectors" |
I was able to pause in the debugger and drill down a bit, but I'm still confused. There's some sort of convention for defining these methods that I'm not familiar with. I'm looking at a user instance in the debugger...I'll try to asciify it :
So if I understand this at all, it seems that anytime I access a property or method of a model instance, there's some javascript magic going on... I saw similarly named methods in the lb-services.js file generated for angularjs... ...What is this magic and how does it work? :) |
The methods for relations are added to the model as properties with getters. For example, user.accessTokens will return a function bound to the user instance. The function itself also has other methods such as ‘create’, ‘count’, and ‘delete’. Please note the ____accessTokens methods are added to the User.prototype for legacy remoting purposes. These methods will probably be removed from the model prototype in the future. Thanks, Raymond Feng StrongLoop http://strongloop.com/ makes it easy to develop APIs http://strongloop.com/mobile-application-development/loopback/ in Node, plus get DevOps capabilities http://strongloop.com/node-js-performance/strongops/ like monitoring, debugging and clustering.
|
Hmm...ok, so if I understand correctly, all the properties and methods in models are defined using
Hmm...sorry about so many tangential questions...I got started and couldn't stop, hehe. Maybe we should move this thread into the google group? ...although, tbh, I rather prefer the markdown support here on github. |
+1 Note that Node v0.12/io.js 1.0 will ship a native implementation of Promises. Thus a possible solution for LoopBack is to use the global I started to look into ways how to add promises to Node core API in nodejs/node#173. IMO it would be best to apply the same approach in loopback, as it involves only about 2-3 changed lines per function. |
- Added https://github.com/petkaantonov/bluebird - All methods now works with promises. Remote methods get called like usual thanks to the awesome .nodeify() - Required modules can be "promisified" with .promisifyAll(module); - Loopback Models can be "promisified" (see strongloop/loopback#418) Result: cleaner, flatter, easier to understand code, better error handling. see http://spion.github.io/posts/why-i-am-switching-to-promises.html Why Bluebird ? see http://www.reddit.com/r/javascript/comments/1r3hwa/which_javascript_prom ise_library_is_the_best_in/
Adding on, what you guys have said, it'd also be nice if remote methods could return promises. |
Actually adding promises to all loopback calls and all |
The loopback angular sdk already returns promises. It is built out on ngResource. For instance, It would be nice to get promises built in to the query syntax as well. |
- Added https://github.com/petkaantonov/bluebird - All methods now works with promises. Remote methods get called like usual thanks to the awesome .nodeify() - Required modules can be "promisified" with .promisifyAll(module); - Loopback Models can be "promisified" (see strongloop/loopback#418) Result: cleaner, flatter, easier to understand code, better error handling. see http://spion.github.io/posts/why-i-am-switching-to-promises.html Why Bluebird ? see http://www.reddit.com/r/javascript/comments/1r3hwa/which_javascript_prom ise_library_is_the_best_in/
@partap so the promises for model relationship still not work by the bluebird plan? |
+1. Could you please post a behavior schema for the future implementation of promises. Since they are a solid future feature for JS we will see a lot of frameworks going towards them. I would like to start using them but I would like my changes to be future proof. How is it going to be? Thanks. |
I have wrapped everything in bluebird -Async promises, If that can influence the roadmap :-) |
This is awesome. Using the techniques here I've been able to use Bluebird promises for most of my loopback code. The only exception I have found so far is the model relation methods which was mentioned earlier in this thread. Just wondering if anyone has come up with a way to promisify the complete model, including the relationship methods? |
Promisifying custom validators in loopback-datasource-juggler is missing from the checklist. I've submitted a PR here: loopbackio/loopback-datasource-juggler#1229 Hoping to get your input @bajtos on what's needed on the testing front. |
Added to the checklist. Thank you for the pull request too! |
It would be great if we could add support for a Promise to be returned for each This will allow us to decide whether to perform a task or not, or even disable parts of the API's functionality, wait before running tests etc. See for example this In summary we need a (lightweight) contract so components can be looked up as |
@anodynos thank you for the comment. Your request makes sense to me, however it's rather off topic in this thread, which is focused on modifying existing APIs to support dual callback/promise. Could you please open a new issue to discuss component registration/discovery? |
I noticed that when using promises with instance methods of
So perhaps support for instance methods on |
@eman6576 that should work just fine. Can you post a reproduction of it? |
This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions. |
@kjdelisle @raymondfeng I think we may need to address this issue as part of our work on LoopBackNext. At least the parts in juggler (e.g. scope methods), because we are reusing juggler in LoopBackNext so far. |
This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions. |
This issue has been closed due to continued inactivity. Thank you for your understanding. If you believe this to be in error, please contact one of the code owners, listed in the |
This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions. |
This issue has been closed due to continued inactivity. Thank you for your understanding. If you believe this to be in error, please contact one of the code owners, listed in the |
LoopBack should provide Promise-enabled API out of the box. The plan is to use the approach outlined in nodejs/node#173.
Tasks
automigrate
,autoupdate
,discover*
, etc.app.listen
@promise
annotation Support @promise annotation strong-docs#63@promise
attributeBelow is the original proposal from @coodoo.
I've been playing around with bluebird's promisifyAll() for a while, ended up I found it just take a couple of line to wrap the model and make it become async-compatible.
See code below:
Once the model is wrapped, we can use Promise syntax instead of callbacks:
Ain't that lovely? :)
Of course it would be even better if all those happened higher up in the chain and become default.
Thought?
The text was updated successfully, but these errors were encountered: