javascript - 如何在nodejs中实现bluebird promise

标签 javascript node.js callback promise bluebird

我尝试了 node bluebird promise,我无法在 then 函数中使用 resolve & reject。 下面是我调用 promise 的代码

  modules.modelClip.exGetAllClips(sUserData)
            .then(function(finalResult) {
                  console.log("Final result " + finalResult)
            })
            .error(function(e) {
                  console.log("Error handler " + e)
            })
            .catch(function(e) {
                  console.log("Catch handler " + e)
            });

并且在 exGetAllClips 函数中返回 promise 。

exports.exGetAllClips = function(pUserData) {
      console.log("---   inside : clipModel : exGetAllClips   -----------------------------------------------------");
      console.log(pUserData);

      return new modules.promise(function(resolve, reject) {
            modules.dbConnection.getConnection(function(rErrorCon, connection) {
                  if (rErrorCon) {
                        reject(rErrorCon);
                  } else {
                        resolve(connection);
                  }
            });
      }).then(function(connection) {
            console.log('Result 4 ')
            var sClipQuery = "CALL spGetAllClips(?)";
            var query = connection.query(sClipQuery, [pUserData.selfId
            ]);
            query.on('error', function(err) {
                  // Handle error, an 'end' event will be emitted after this as well
                  //return err;
                  console.log(" error : spGetAllClips : ",err);
                 reject(err);
            }).on('result', function(row) {
                  console.log("row : ", JSON.stringify(row));
                  resolve( row);
            }).on('end', function() {
                  // all rows have been received
                  connection.release();
            })

      });
};

如果存在,我想从 .then 中抛出错误。但是无法执行此操作,它会抛出错误 reject is undefined

请帮助,如何实现这个或任何其他方式。

最佳答案

我希望这样,首先有两个回调(一个实际上是一个事件处理程序,我不太习惯使用 Promises 来处理它),所以将它们分成两个 promise :

  • 通过 promise 整个模块,使用 getConnectionAsync 而不是 getConnection

  • 关注bluebird docs用于处理一次性事件(请注意,我没有维护一个标志来检查 promise 是否已经解决),代码可能是这样的:

     modules.dbConnection = modules.promise.promisifyAll(modules.dbConnection);
     ...
    
     exports.exGetAllClips = function(pUserData) {
       console.log("---   inside : clipModel : exGetAllClips   -----------------------------------------------------");
       console.log(pUserData);
    
       return modules.dbConnection.getConnectionAsync()
         .then(function(connection) {
           console.log('Result 4 ')
           var sClipQuery = "CALL spGetAllClips(?)";
           return new modules.promise(function(resolve, reject){
               var query = connection.query(sClipQuery, [pUserData.selfId]);
               query.on('error', function(err) {
                 // Handle error, an 'end' event will be emitted after this as well
                 //return err;
                 console.log(" error : spGetAllClips : ",err);
                  reject(err);
               }).on('result', function(row) {
                 console.log("row : ", JSON.stringify(row));
                 resolve( row);
               }).on('end', function() {
                 // all rows have been received
                 connection.release();
               });
           });
     };
    

关于javascript - 如何在nodejs中实现bluebird promise,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35332333/

相关文章:

javascript - 在点击事件中显示日历

javascript - 如何在滚动条上循环垂直轮播

node.js - Mongoose 仅​​在更改时验证

c++ - 函数指针和回调

C++:无法使用 Mini-XML 从 XML 文件加载长字符串

java - Android 上的回调函数

javascript - 从长写改为 jQuery 循环

php - 使用PHP阻止用户下载代码,但代码是cpu密集型的

node.js - Morgan 在调用路由器后记录请求

node.js - Sinon 函数 stub : How to call "own" function inside module