users.userExists({
        username: username
      }, function(err, exists) {
        if (err) {
          users.close();
          console.error('Error testing if user exists: ' + err);
          response.error = '业务忙,请稍候重试';
          res.status(HttpStatus.INTERNAL_SERVER_ERROR).json(response);
          return;
        } else if (exists) {
          users.close();
          response.error = '用户名已经存在';
          res.status(HttpStatus.CONFLICT).json(response);
          return;
        } else {
          users.emailExists(profile.email, function(err, exists) {
            console.log(profile.email +' ' + exists);
            if (err) {
              users.close();
              console.error('checking email error');
              response.error = '业务忙,请稍候重试';
              res.status(HttpStatus.INTERNAL_SERVER_ERROR).json(response);
              return;
            } else if (exists) {
              users.close();
              response.error = '邮箱 ' + profile.email + ' 已存在';
              res.status(HttpStatus.CONFLICT).json(response);
              return;
            }
            users.createUser(username, password, profile, function(err, uid, hash) {
              if (err) {
                response.error = '生成用户错误';
                console.error('Failed to create user ' + err);
                res.status(HttpStatus.INTERNAL_SERVER_ERROR).json(response);
              } else {
                response.statusMessage = '成功生成用户';
                response.uid = uid;
                // best-effort without a callback
                res.json(response);
              }
            });
          });
        }
      })
|      1zwh8800      2016-07-22 14:45:59 +08:00 用 async + await | 
|      2zealic      2016-07-22 14:48:20 +08:00 你都知道 callback hell 了,为什么不知道 Promise | 
|  |      3shiny      2016-07-22 14:50:56 +08:00 Promise 模式,还可以加点 ES2015 语法糖会更甜。 | 
|  |      4zenxds      2016-07-22 14:57:56 +08:00 | 
|  |      5Mirachael      2016-07-22 15:15:22 +08:00 挖坟贴? | 
|  |      6plqws      2016-07-22 15:17:40 +08:00 这种程度的 hell 还好啦 按照楼主这样子的代码, async await 可能还得配合 tuple 使用… | 
|  |      7zhuangzhuang1988      2016-07-22 15:18:26 +08:00  1 如果闲的蛋疼的话可以试试 http://fsprojects.github.io/Fable/ 最后结果类似这样 ```F# async { try let! exists = users.userExists({username: username}) if exists: res.status(HttpStatus.CONFLICT).json(response); return; try: let! users.emailExists(profile.email) if exists: response.error = '邮箱 ' + profile.email + ' 已存在'; res.status(HttpStatus.CONFLICT).json(response); return try: let! (uid, hash) = users.createUser(username, password, profile) response.statusMessage = '成功生成用户'; response.uid = uid; res.json(response); except err: response.error = '生成用户错误'; console.error('Failed to create user ' + err); res.status(HttpStatus.INTERNAL_SERVER_ERROR).json(response); except err: console.error('checking email error'); response.error = '业务忙,请稍候重试'; res.status(HttpStatus.INTERNAL_SERVER_ERROR).json(response); except err: console.error('Error testing if user exists: ' + err); response.error = '业务忙,请稍候重试' res.status(HttpStatus.INTERNAL_SERVER_ERROR).json(response); finally: users.close() } ``` | 
|  |      8zhuangzhuang1988      2016-07-22 15:18:55 +08:00 擦 , 空格都没了 | 
|      9hawklim      2016-07-22 16:20:39 +08:00 co 模块 | 
|      10DoraJDJ      2016-07-22 16:36:57 +08:00 via Android Promise 、 Generator 、 co 、 async+await 总有一款适合你 | 
|  |      11ThinkCat      2016-07-22 17:23:55 +08:00 Promise + Generator + co 看起来优雅点,然而概念让人乱乱的 | 
|  |      12ccbikai PRO 目前最合适的是 co |