繁体   English   中英

(JavaScript) 如何从另一个 function 返回结果?

[英](JavaScript) How to return result from another function?

图像打印屏幕 ---------

平台: NodeJS

我在nodejs项目中有两个角色我无法将第二个function的结果插入第一个function。

如何在第一个 function 启动(客户端)中插入 RUN function 的结果?

function start (client)
...
.sendText (message.from, 'Example' + result.rows + 'Text text')
...


function run ()
...
Console.log (result.rows);
...

完整代码

'use strict';
const venom = require('venom-bot');
const oracledb = require('oracledb');
const dbConfig = require('./dbconfig.js');

venom
.create()
.then((client) => start(client))
.catch((erro) => { console.log(erro); });

function start(client)
 {
  client.onMessage((message) =>
  { 
  if (message.body === 'Vendas' && message.isGroupMsg === false) 
     {    client
        .sendText(message.from, 'Example text' + result.rows + 'Etc etc') 
        .then((result) => {console.log('Result: ', result); }) 
        .catch((erro) => { console.error('Error when sending: ', erro); }); 
}});
}
//----------------------------------------------------------------
async function run() {
  let connection;
  try {
    connection = await oracledb.getConnection(dbConfig);
    const sql =`SELECT 'Testeee' FROM dual`;
    let result;
    result = await connection.execute(sql);
     Console.log(result.rows);
    
 } catch (err) {
    console.error(err);
  } finally {
    if (connection) {
      try {
   
        await connection.close();
      } catch (err) {
        console.error(err);
      }
    }
  }
}

尝试使start()异步并应用wait

async function start(client){
    const result = await run();

    // the rest of your code
}

run()中,您让方法返回值。

async function run() {
   let connection;
   try {
      connection = await oracledb.getConnection(dbConfig);
      const sql =`SELECT 'Testeee' FROM dual`;
      return await connection.execute(sql);
} // rest of your code

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM