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

使用Bluebird手动实现pg.connect

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

使用Bluebird手动实现pg.connect

丢掉所有可怕的回调代码,然后在应用程序初始化的某个位置执行此操作:

var pg = require("pg");var Promise = require("bluebird");Object.keys(pg).forEach(function(key) {    var Class = pg[key];    if (typeof Class === "function") {        Promise.promisifyAll(Class.prototype);        Promise.promisifyAll(Class);    }})Promise.promisifyAll(pg);

以后在任何地方都可以使用pg模块,就好像它被设计为使用promises开头:

// Later// Don't even need to require bluebird herevar pg = require("pg");// Note how it's the pg API but with *Async suffixpg.connectAsync(...).spread(function(connection, release) {     return connection.queryAsync("...")         .then(function(result) { console.log("rows", result.rows);         })         .finally(function() { // Creating a superfluous anonymous function cos I am // unsure of your JS skill level release();         });});


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

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

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