From db0e771daded652a89e447c7cd70371f7a075120 Mon Sep 17 00:00:00 2001 From: Paul Selden Date: Mon, 6 Jan 2014 14:36:27 -0500 Subject: [PATCH] Refactor parallel post/user get. --- platform/posts/postsPersistence.js | 16 ++++++++++++---- 1 file changed, 12 insertions(+), 4 deletions(-) diff --git a/platform/posts/postsPersistence.js b/platform/posts/postsPersistence.js index 0865f8d..e6261d0 100644 --- a/platform/posts/postsPersistence.js +++ b/platform/posts/postsPersistence.js @@ -1,10 +1,18 @@ var db = require('../db'); exports.createPost = function *(userId, title){ - var results = yield [ db.sequelize.Post.create({ title: title }), db.sequelize.User.find(userId) ]; - var post = results[0]; - var user = results[1]; - yield user.setPosts([post]); + var tasks = yield { + post: db.sequelize.Post.create({ title: title }), + user: db.sequelize.User.find(userId) + }; + + var user = tasks.user; + var post = tasks.post; + + if(user && post){ + yield user.setPosts([post]); + } + return post; };