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
})
})
})
})
}如何将chatRoom对象(这是嵌套承诺的结果)返回主函数?
发布于 2016-08-19 09:57:26
不要忘了回报承诺,以便被锁上链子。
function matchUserToChatRoom(userId) {
return models.User.findOne({where: {messenger_id: userId}})
.then(function(user) {
return models.ChatRoom
.findOrCreate({where: {status: "open"}, defaults: {status: "open"}})
.spread(function(chatRoom, created) {
return chatRoom.addUser(user);
})
})
})
}发布于 2016-08-19 09:56:06
这是为了得到灵感。您也需要使用reject。
function matchUserToChatRoom(userId) {
return new Promise(function(resolve, reject){
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) {
resolve(chatRoom);
//FIXME: I want to use this "chatRoom" inside the main function
})
})
})
})
}
});https://stackoverflow.com/questions/39035993
复制相似问题