如何在Node.js中链接两个promise?

时间:2014-08-12 21:31:45

标签: javascript node.js promise q

我是Node.js的新手。我想链接两个承诺。当第一个承诺失败时,不应执行第二个承诺。但是,当第一个承诺失败时,我的代码总是执行第二个承诺。

function genQueryPromise(query, parameters) {
  var deferred = Q.defer();
  dbClient.executeAsPrepared(query, parameters,
    function(err) {
      if (err) {
        console.log(
            'error in query: ' + query + ", parameters: " + parameters);
        deferred.reject(new Error(err));
      } else {
        console.log("Query succeeded: " + query);
        deferred.resolve();
      }
    }
  );
  return deferred.promise;
}

var timeUuid = cql.types.timeuuid();
genQueryPromise(
    'INSERT INTO points_statement ' +
      '(user_id, statement_id, points_change, time_updated, note) ' +
    'VALUES (?, ?, ?, dateof(now()), ?)',
    ["mytest8",
     timeUuid,
     220,//{value: pointsChange, hint: cql.types.dataTypes.bigint},
     "test"]
  )
  .then(genQueryPromise(
    'UPDATE user_points SET points = points + ? WHERE user_id = ?',
    [{value: 220, hint: cql.types.dataTypes.bigint}, "mytest8"]
  ))
  .fail(function (error) {
    console.log("db error " + error);
  });

输出如下:

error in query: INSERT INTO points_statement (user_id, statement_id, points_change, time_updated, note) VALUES (?, ?, ?, dateof(now()), ?), parameters: mytest8,b5835850-2266-11e4-a871-f1a82b5a7753,220,test
db error Error: ResponseError: Expected 8 or 0 byte long (4)
Query succeeded: UPDATE user_points SET points = points + ? WHERE user_id = ?

您可以看到第二个承诺即使第一个承诺失败也会执行。我做错了什么?感谢。

1 个答案:

答案 0 :(得分:0)

在构建承诺链时,您正在调用genQueryPromise。在链中呼叫:

genQueryPromise(
    'INSERT INTO points_statement ' +
      '(user_id, statement_id, points_change, time_updated, note) ' +
    'VALUES (?, ?, ?, dateof(now()), ?)',
    ["mytest8",
     timeUuid,
     220,//{value: pointsChange, hint: cql.types.dataTypes.bigint},
     "test"]
  )
  .then(function() {
    return genQueryPromise(
      'UPDATE user_points SET points = points + ? WHERE user_id = ?',
      [{value: 220, hint: cql.types.dataTypes.bigint}, "mytest8"]
    );
  })
  .fail(function (error) {
    console.log("db error " + error);
  });

添加一个函数包装器使第二次调用在第一次调用完成后执行,而不是同时执行。