Node.js的和符合Q异步编程 [英] Node.js and asynchronous programming with Q

查看:182
本文介绍了Node.js的和符合Q异步编程的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是下列正确的方式去了解的东西呢?这是一个标志了控制器动作。进出口创建用户和组,将用户添加到。请注意,我有方法级变量称为用户和组。的code的其余部分是异步使用Q模块

它是确定有方法级变量或将它们通过被覆写的其他人在同一时间报名?

  exports.postSignUp =功能(REQ,资源,下一个){    VAR用户,
        组;    返回Q.invoke(出口,'parseUser',REQ,NULL)
        。然后(功能(U)
        {
            用户= U;
            返回Q.invoke(出口,'postCreateUser',用户).fail(功能(错误){抛出犯错;});
        })
        。然后(函数()
        {
            组=新集团();
            group.userIds = [user._id];
            返回Q.invoke(出口,'postCreateGroup',组).fail(功能(错误){抛出犯错;});
        })
        。然后(函数()
        {
            user.groupId = group._id;
            group.userIds = [user._id];
        })
        。然后(函数()
        {
            返回Q.ninvoke(REQ,登录,用户).fail(功能(错误){抛出犯错;});
        })
        。然后(函数()
        {
            返回res.redirect('/旅游');
        })
        .fail(功能(错误)
        {
            的console.log('U:'+用户);
            的console.log('G:'+组);
            返回exports.createValidationError(错误,REQ,资源,用户);
        });
};


解决方案

  

它是确定有方法级变量或将它们通过被覆写的其他人在同一时间报名?


没有,他们不会因为它们是局部范围到你的 postSignUp 功能。

这不过是有点难看,它们筑巢可能是更好的选择在这里:

  exports.postSignUp =功能(REQ,资源,下一个){
    返回Q.invoke(出口,'parseUser',REQ,空)。然后(功能(用户){
        返回Q.invoke(出口,'postCreateUser',用户)。然后(功能(createdUser){
            VAR组=新集团();
            group.userIds = [user._id];
            返回Q.invoke(出口,'postCreateGroup',组)。然后(功能(createdGroup){
                user.groupId = group._id;
                group.userIds = [user._id]; //重复这个?                返回Q.ninvoke(REQ,登录,用户)
            }),然后(功能(与loggedInUser){
                返回res.redirect('/旅游');
            })失败(函数(ERR){
               的console.log('G:'+组);
               扔犯错;
            });
        })失败(函数(ERR){
            的console.log('U:'+用户);
            返回exports.createValidationError(错误,REQ,资源,用户);
        });
    })// .fail(功能(错误){
       // ... // parseUser或createValidationError失败
       //});
};


  

我真的需要打破所有不同的步骤,进入不同的功能或我可以有1异步功能?


您当前1异步功能,您可能要打破下来到创造一个新的组的用户,并记录了他在加重定向他 /旅游

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.

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.

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?

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.

这篇关于Node.js的和符合Q异步编程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆