function main() {
matchUserToChatRoom(senderID)
.then(function(chatRoom) {
//FIXME: I want to be able to use chatRoom from the below function
console.log(chatRoom)
})
}
function matchUserToChatRoom(userId) {
return models.User.findOne({where: {messenger_id: userId}})
.then(function(user) {
models.ChatRoom
.findOrCreate({where: {status: "open"}, defaults: {status: "open"}})
.spread(function(chatRoom, created) {
chatRoom.addUser(user).then(function(chatRoom) {
//FIXME: I want to use this "chatRoom" inside the main function
})
})
})
})
}
How do I return the chatRoom
object, which is a result of nested promises, to the main function?