zoukankan      html  css  js  c++  java
  • node.js异步编程解决方案之Promise用法

    node.js异步编程解决方案之Promise

    var dbBase = require('../db/db_base');
    var school_info_db = require('../db/school_info_db');
    
    function dbService(wxCorpid) {
        return new Promise(function(resolve, reject) {
                school_info_db.get_school_info(wxCorpid, function(err, val) {
                    resolve(val);
                });
            })
            .then(function(schoolInfo) {
                return new Promise(function(resolve, reject) {
                    resolve(dbBase.init(schoolInfo.dbConnString));
                })
            })
            .catch(function(error) {
                // 处理前面Promise产生的错误
                console.log('数据库连接池创建失败');
            });
    }
    
    async function optErrLog(wxCorpid) {
        var mysql = await dbService(wxCorpid);
        Promise.resolve(mysql)
            .then(function(mysql) {
                return new Promise(function(resolve, reject) {
                    mysql.query('select * from bas_school;', function(err, rlt, feld) {
                        resolve(rlt);
                    })
                })
            })
            .then(result => console.log(JSON.stringify(result)))
    }
    
    module.exports = {
        optErrLog: optErrLog
    }
  • 相关阅读:
    PHP中each与list用法分析
    三大范式通俗讲解
    数据库三大范式详解
    利用JS制作简便计算器
    CSS下拉列表错误纠正
    下拉列表
    CSS选择器、标签,div的位置。
    php注册审核
    php分页查询
    php投票练习
  • 原文地址:https://www.cnblogs.com/xbblogs/p/7545177.html
Copyright © 2011-2022 走看看