db2 node. js/ JavaScript / SQL with numc/await -不同步

col17t5w  于 12个月前  发布在  DB2
关注(0)|答案(3)|浏览(218)

我试图调用函数,使SQL调用并返回数据到调用函数我试图使用node.js/JavaScript中的promises/await语法失败我的函数仍然异步运行。
也许不太理解这是如何工作的。

const {dbconn, dbstmt} = require('idb-pconnector');

async function get_data(){

        const {dbconn, dbstmt} = require('idb-connector');
        const sql_stsmt = 'SELECT * FROM QIWS.QCUSTCDT';
        const conn1 = new dbconn();
        conn1.conn('*LOCAL');
        const stmt = new dbstmt(conn1);

        try {
            await stmt.exec(sql_stsmt, (x) => {
            stmt.close();
            conn1.disconn();
            conn1.close();            
            // getting expected data here..
            console.log("1..")     
            console.log("%s", JSON.stringify(x));                
            return x;
            });
        }
        catch (error) {
            return console.error(error);            
            }
    }

async function main() {
    try{
        const result = await get_data()
        console.log("2..")     
        // still coming out undefined here...
        console.log("%s", JSON.stringify(result));                
        return result;
    }
    catch(error){
        return console.error(error);            
    }
}

let data = main();
console.log("3..")     
// coming out empty {} here.
console.log("%s", JSON.stringify(data));

字符串

aurhwmvo

aurhwmvo1#

你应该 Package exec方法简单的例子:

function execWithCallback(callback: (x: number, err: Error | null) => void) {
  setTimeout(() => {
    callback(1, null)
  }, 1000)
}

async function execPromise() {
  return new Promise((resolve, reject) => {
    execWithCallback((x, err) => {
      if(err) return reject(err)

      return resolve(x)
    })
  })
}

字符串

vulvrdjw

vulvrdjw2#

很多好的想法和阅读。感谢基思和VLAZ能够修改。现在工作。(但是,仍然可能需要一些错误捕获逻辑的帮助。)

const {Connection, Statement} = require('idb-pconnector');

async function get_data(){

        const sql_stsmt = 'SELECT * FROM QIWS.QCUSTCDT';        
        const connection = new Connection({ url: '*LOCAL' });
        const stmt = new Statement(connection);
        
        try {
            const x = await stmt.exec('SELECT * FROM QIWS.QCUSTCDT');                
            stmt.close();
            connection.disconn();
            connection.close();            
 
            return x;
        }
        catch (error) {
            return console.error(error);            
            }
    }

async function main() {
    try{
        const result = await get_data()
        console.log("log from function main()");
        console.log("%s", JSON.stringify(result));                
        // now we can present data on web page...
        // ...
    }
    catch(error){
        return console.error(error);            
    }
}

main();

字符串

kwvwclae

kwvwclae3#

你正在尝试做的事情无法在JavaScript中完成。
=编辑=
async and await不会从根本上改变promise在JavaScript中的工作方式,也不会改变它是单线程的。
异步函数返回一个Promise,在非异步函数中获取这个Promise结果的唯一方法是在Promise上调用then

let data = main.then(function (result) {
  console.log("%s", JSON.stringify(result)); 
});
console.log("3..")     
// coming out empty {} here.

字符串

var async_func = () => {
  var p = new Promise((result) => {
    setTimeout(() => {
      result("foo");
    });
  });
  return p;
}

var func1 = () => {
  console.log("func1: " + async_func());
}

var func2 = () => {
  async_func().then((res) => {
    console.log("func2: " + res);
  });
}
var func3 = async () => {
  var x = await async_func();
  console.log("func3: " + x);
}

func3();
func1();
func2();

相关问题