Node,RSVP.js承诺数组

时间:2014-02-21 09:03:27

标签: javascript node.js promise rsvp.js

我可以获得RSVP数组的承诺:

//if user wants to change username
var promises = [];

promises['username'] = new RSVP.Promise(function(resolve, reject){ 

 if(user.username !== session.username){

    //check if username is available
    $model_users.getBy({
      what:'id',
      where: " username = "+db.escape(user.username) +" AND id != "+ db.escape(session.id_user)
    })
    .then(function(results){

    if(!results || results.length <= 0){
      //update username is available
      $model_users.update({
        update:{username: db.escape(user.username)},
        where: " id = "+ db.escape(session.id_user)

      })
      .then(function(success){

        confirm.username = 'was_saved';

        resolve(success);

      },function(err){

        reject(err);

        console.log('db-error 55299 ' + err);
        res.send('db-error');
      });

    }else{
      validation.username = 'error_username_exists';
      resolve(validation);
      res.send({"validation":validation});
    }

    },function(err){

    reject(err);

    console.log('db-error 0299 ' + err);
    res.send('db-error');

    });

}else{
  reject('no username to update - is same to session username');
}

}

 //create new JWT and send it back to FE localSTorage
RSVP.all(promises).then(function(){

  $model_users.getBy({
      what:'id , username',
      where: " id = "+ db.escape(session.id_user)
    }).then(function(results){

      if(results && results.length > 0){
        //set new user session and token
        var auth = {};

        auth.username = results[0].username;
        auth.id_user = results[0].id;
        auth.session_token =  jwt.sign(auth, config.session_secret_key, { expiresInMinutes: config.session_expiration });

        res.send({"auth":auth,"confirm":confirm});

      }else{

      res.send('db-error');

      }

    },function(err){

     console.log('db-error 0.577 '+ err);
    });
});

返回的错误是:

RSVP.all(promises).then(function(){
^^^^
SyntaxError: Unexpected identifier

他的可能性如何?我正在关注官方文件https://github.com/tildeio/rsvp.js/#arrays-of-promises

我将它设置在节点app.js:

之上

var RSVP = require('rsvp');

1 个答案:

答案 0 :(得分:1)

您错过)以上RSVP.all(promises)...两行。

考虑使用linter,它会立即捕获此类错误。参见例如SublimeLinter