Closed
Description
LoopBack should provide Promise-enabled API out of the box. The plan is to use the approach outlined in nodejs/node#173.
Tasks
- Support Promise-returning method and hooks strong-remoting#179 support promises in strong-remotin
- Promises for operation hooks loopbackio/loopback-datasource-juggler#449 loopback-datasource-juggler - support promises in "operation hooks"
- Promisify DAO loopbackio/loopback-datasource-juggler#451 and Related Models Promisification loopbackio/loopback-datasource-juggler#452 loopback-datasource-juggler - convert all DAO & relation methods to dual callback/promise API
- promisify juggler's DataSource methods like
automigrate
,autoupdate
,discover*
, etc. - promisify juggler's relation and scope methods
- promisify custom validators in juggler
- WIP: Custom validator promises and options loopbackio/loopback-datasource-juggler#1229
- convert loopback builtin models to support dual callback/promise API
- User - Promisify User model #1493
- Application - Promisify 'Application' model #1533
- PersistedModel (replication, change tracking) - Promisify 'PersistedModel - replication' #1536
- Change
- Checkpoint
- Scope
- Model.checkAccess()
- AccessToken
- Role (and role resolvers)
- RoleMapping
- ACL
- Promisify
app.listen
- Modify connectors and convert all mixin methods to dual callback/promise API
- loopback-connector-remote
- Email model & connector
- (?) Allow middleware handlers to return a promise
- Support promises in loopback-phase, both for API and for handler functions
- Update examples and docs
- Enhance strong-docs to support
@promise
annotation Support @promise annotation strong-docs#63 - Edit API docs / Mark all functions supporting promises with
@promise
attribute - Wiki docs with basic desc of new promise support (differences, etc) Concept / task docs for using LB Promise APIs #1575
- Add / modify example apps using Promises Add or modify examples using Promises API #1576
- Enhance strong-docs to support
Below 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:
var Promise = require("bluebird");
module.exports = function(ResetKey) {
// once a model is attached to the data source
ResetKey.on('dataSourceAttached', function( obj ){
// wrap the whole model in Promise
// but we need to avoid 'validate' method
ResetKey = Promise.promisifyAll( ResetKey, {filter: function(name, func, target){
return !( name == 'validate');
}} );
})
}
Once the model is wrapped, we can use Promise syntax instead of callbacks:
// old way - callbacks
user.save(function(err, result){...})
// new way - promise
user.saveAsync()
.then(function(result){...})
.catch(function(err){...})
Ain't that lovely? :)
Of course it would be even better if all those happened higher up in the chain and become default.
Thought?