简体   繁体   English

Node.js和Q异步编程

[英]Node.js and asynchronous programming with Q

Is the following the correct way to go about things? 以下是处理事情的正确方法吗? This is a sign up controller action. 这是一个注册控制器操作。 Im creating a user and a group to add the user to. 林创建一个用户和一个组来添加用户。 Notice I have method level variables called user and group. 注意,我有方法级别的变量,称为用户和组。 The rest of the code is asynchronous using the Q module. 使用Q模块,其余代码是异步的。

Is it ok to have the method level variables or will they be overriden by the another person signing up at the same time? 可以使用方法级别的变量吗?或者同时注册的另一个人可以覆盖它们?

exports.postSignUp = function(req, res, next) {

    var user,
        group;

    return Q.invoke(exports, 'parseUser', req, null)
        .then(function(u)
        {
            user = u;
            return Q.invoke(exports, 'postCreateUser', user).fail(function(err) { throw err; });
        })
        .then(function()
        {
            group = new Group();
            group.userIds = [user._id];
            return Q.invoke(exports, 'postCreateGroup', group).fail(function(err) { throw err; });
        })
        .then(function()
        {
            user.groupId = group._id;
            group.userIds = [ user._id ];
        })
        .then(function()
        {
            return Q.ninvoke(req, 'login', user).fail(function(err) { throw err; });
        })
        .then(function()
        {
            return res.redirect('/tour');
        })
        .fail(function (err)
        {
            console.log('u:' + user);
            console.log('g:' + group);
            return exports.createValidationError(error, req, res, user);
        });
};

Is it ok to have the method level variables or will they be overriden by the another person signing up at the same time? 可以使用方法级别的变量吗?或者同时注册的另一个人可以覆盖它们?

No, they won't as they are locally scoped to your postSignUp function. 不,它们不会,因为它们在本地范围内属于您的postSignUp函数。

It is however a little ugly, nesting them might be the better choice here: 但是,这有点丑陋,将它们嵌套在这里可能是更好的选择:

exports.postSignUp = function(req, res, next) {
    return Q.invoke(exports, 'parseUser', req, null).then(function(user) {
        return Q.invoke(exports, 'postCreateUser', user).then(function(createdUser) {
            var group = new Group();
            group.userIds = [user._id];
            return Q.invoke(exports, 'postCreateGroup', group).then(function(createdGroup) {
                user.groupId = group._id;
                group.userIds = [ user._id ]; // repeat this?

                return Q.ninvoke(req, 'login', user)
            }).then(function(loggedInuser) {
                return res.redirect('/tour');
            }).fail(function(err) {
               console.log('g:' + group);
               throw err;
            });
        }).fail(function(err) {
            console.log('u:' + user);
            return exports.createValidationError(error, req, res, user);
        });
    }) // .fail(function(err) {
       //     … // parseUser or createValidationError failed
       // });
};

Do I really need to break down all the different steps into different functions or can I just have 1 async function? 我是否真的需要将所有不同的步骤分解为不同的功能,或者我是否只有1个异步功能?

You have one async function currently, you might want to break that down into creating a user with a new group, and logging him in plus redirecting him to /tour . 当前,您具有一个异步功能,您可能希望将其分解为使用新组创建一个用户,然后登录并将其重定向到/tour

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM