Trying to use Promises instead of caolan's async library, I'm struggling with completely different approach. A little clarification is highly appreciated.
Let's say I'm registering User with Sequezlie library. To do this I need:
- Create user oject
- Create dependent object Folder
- Return result to client
Sequezlie use promises for all CRUD-operations. And User.register is a promisifed shortcut to User.create(...)
.
user =
email: req.body.email
name : req.body.name
createFolder = (user)->
new Promise (resolve, reject)->
Folder
.build(title: "untitled", UserId: user.id)
.save()
.then (folder)-> resolve folder
.catch (err)-> reject err
# register
User
.register(user, req.body.password)
.then(createFolder(user))
.then (folder)->
console.log "User with Folder is created"
res.send 201
At this time Folder is created, but createFolder() has no access to user
object. What I'm doing wrong? Which is the better way to chain Promises and access it's results in each other?
FYI, I'm using then/promise implementation.
Thanks.