• <tfoot id='8lnoO'></tfoot>

      <bdo id='8lnoO'></bdo><ul id='8lnoO'></ul>

    1. <small id='8lnoO'></small><noframes id='8lnoO'>

    2. <legend id='8lnoO'><style id='8lnoO'><dir id='8lnoO'><q id='8lnoO'></q></dir></style></legend>
        <i id='8lnoO'><tr id='8lnoO'><dt id='8lnoO'><q id='8lnoO'><span id='8lnoO'><b id='8lnoO'><form id='8lnoO'><ins id='8lnoO'></ins><ul id='8lnoO'></ul><sub id='8lnoO'></sub></form><legend id='8lnoO'></legend><bdo id='8lnoO'><pre id='8lnoO'><center id='8lnoO'></center></pre></bdo></b><th id='8lnoO'></th></span></q></dt></tr></i><div id='8lnoO'><tfoot id='8lnoO'></tfoot><dl id='8lnoO'><fieldset id='8lnoO'></fieldset></dl></div>

        一个promisified mysql 模块将如何与NodeJS 一起工作?

        时间:2024-04-15

      1. <i id='ucllz'><tr id='ucllz'><dt id='ucllz'><q id='ucllz'><span id='ucllz'><b id='ucllz'><form id='ucllz'><ins id='ucllz'></ins><ul id='ucllz'></ul><sub id='ucllz'></sub></form><legend id='ucllz'></legend><bdo id='ucllz'><pre id='ucllz'><center id='ucllz'></center></pre></bdo></b><th id='ucllz'></th></span></q></dt></tr></i><div id='ucllz'><tfoot id='ucllz'></tfoot><dl id='ucllz'><fieldset id='ucllz'></fieldset></dl></div>

          <small id='ucllz'></small><noframes id='ucllz'>

          <legend id='ucllz'><style id='ucllz'><dir id='ucllz'><q id='ucllz'></q></dir></style></legend>
          1. <tfoot id='ucllz'></tfoot>

                <bdo id='ucllz'></bdo><ul id='ucllz'></ul>

                    <tbody id='ucllz'></tbody>

                  本文介绍了一个promisified mysql 模块将如何与NodeJS 一起工作?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我正在尝试在 NodeJS 中使用 MySQL.我的整个应用程序都是用 promise 构建的,所以我也想对 mysql 模块进行 promisify.

                  I'm trying to work with MySQL in NodeJS. My entire app is built with promises, so I want to promisify the mysql module as well.

                  所以我有这个:

                  Promise = require('bluebird');
                  var mysql = Promise.promisifyAll(require('mysql'));
                  

                  现在,根据他们的 API,connect() 方法接受一个参数,一个 err 回调,在连接错误的情况下被调用.我的问题是,这如何转化为承诺?

                  Now, according to their API, the connect() method accepts a single parameter, an err callback to be called in case of connection error. My question is, how does that translate to promises?

                  承诺会在错误时得到解决吗?会被拒绝吗?我可能需要 .catch() 吗?这是如何运作的?

                  Will the promise be resolved on error? Will it be rejected? Will I need to .catch() it perhaps? How does that work?

                  推荐答案

                  如果一个方法是一个带有单个参数的节点errback" - 它将在 then 中不带任何参数的情况下被解析,或者替代被传递给它的 err 拒绝.在 promisification 的情况下,您可以使用 .error 捕获它或使用 Promise.OperationalError 捕获它.

                  If a method is a node "errback" with a single argument - it will be resolved with no parameters in the then or alternatively be rejected with the err passed to it. In the case of promisification, you can catch it with .error or use a catch with Promise.OperationalError.

                  这是一个简单的方法:

                  function getConnection(){
                      var connection = mysql.createConnection({
                        host     : 'localhost',
                        user     : 'me',
                        password : 'secret'
                      });
                      return connection.connectAsync().return(connection); // <- note the second return
                  }
                  
                  getConnection().then(function(db){
                      return db.queryAsync(....);
                  }).error(function(){
                     // could not connect, or query error
                  });
                  

                  如果这是为了管理连接 - 我会使用 Promise.using - 这是来自 API 的示例:

                  If this is for managing connections - I'd use Promise.using - here is a sample from the API:

                  var mysql = require("mysql");
                  // uncomment if necessary
                  // var Promise = require("bluebird");
                  // Promise.promisifyAll(mysql);
                  // Promise.promisifyAll(require("mysql/lib/Connection").prototype);
                  // Promise.promisifyAll(require("mysql/lib/Pool").prototype);
                  var pool  = mysql.createPool({
                      connectionLimit: 10,
                      host: 'example.org',
                      user: 'bob',
                      password: 'secret'
                  });
                  
                  function getSqlConnection() {
                      return pool.getConnectionAsync().disposer(function(connection) {
                          try {
                              connection.release();
                          } catch(e) {};
                      });
                  }
                  
                  module.exports = getSqlConnection;
                  

                  哪个会让你做:

                  Promise.using(getSqlConnection(), function(conn){
                      // handle connection here, return a promise here, when that promise resolves
                      // the connection will be automatically returned to the pool.
                  });
                  

                  这篇关于一个promisified mysql 模块将如何与NodeJS 一起工作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:视图上的 TSQL 外键? 下一篇:如何从 Python 中的数据库创建 CSV 文件?

                  相关文章

                  <tfoot id='tpkA9'></tfoot>

                  <small id='tpkA9'></small><noframes id='tpkA9'>

                    <legend id='tpkA9'><style id='tpkA9'><dir id='tpkA9'><q id='tpkA9'></q></dir></style></legend>
                      <bdo id='tpkA9'></bdo><ul id='tpkA9'></ul>

                    1. <i id='tpkA9'><tr id='tpkA9'><dt id='tpkA9'><q id='tpkA9'><span id='tpkA9'><b id='tpkA9'><form id='tpkA9'><ins id='tpkA9'></ins><ul id='tpkA9'></ul><sub id='tpkA9'></sub></form><legend id='tpkA9'></legend><bdo id='tpkA9'><pre id='tpkA9'><center id='tpkA9'></center></pre></bdo></b><th id='tpkA9'></th></span></q></dt></tr></i><div id='tpkA9'><tfoot id='tpkA9'></tfoot><dl id='tpkA9'><fieldset id='tpkA9'></fieldset></dl></div>