栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

承诺的mysql模块如何与NodeJS一起使用?

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

承诺的mysql模块如何与NodeJS一起使用?

如果某个方法是带有单个参数的节点“
errback”-它将在中没有任何参数地进行解析,

then
或者
err
通过传递给它而被拒绝。如果是promisification,则可以使用
.error
或使用
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的示例:

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.});


转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/408351.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号