简体   繁体   English

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

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

imagem printscreen ---------图像打印屏幕 ---------

Platform: NodeJS平台: NodeJS

I have two roles in the nodejs project I'm having trouble inserting the result of the second function into the first function.我在nodejs项目中有两个角色我无法将第二个function的结果插入第一个function。

How to insert the result of the RUN function within the first function start (client)?如何在第一个 function 启动(客户端)中插入 RUN function 的结果?

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


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

Full code完整代码

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

Try making start() async either and apply wait .尝试使start()异步并应用wait

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

    // the rest of your code
}

In run() you let the method return the value.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.

相关问题 如何从服务文件返回或导出函数 javascript 的结果以在另一个文件中使用 - How to return or export result of function javascript from service file to use in another file 如何从另一个函数返回“addEventListener()” - Javascript - How to return "addEventListener()" from another function - Javascript 如何从另一个JavaScript文件中的另一个函数读取返回值 - How to read a return value from another function in another javascript file 如何将 function 的结果从外部 javascript 脚本返回到 pug 文件? - How return the result of a function from an external javascript script to a pug file? 从Java中的PageMethods成功函数返回结果 - Return result from PageMethods success function in Javascript Javascript退出功能,并从另一个功能返回 - Javascript exit function with return from another function 如何返回结果值 out 函数 JavaScript - How to return result value out function JavaScript 如何返回onclick函数是javascript中href的结果? - How to return the onclick function is result to href in javascript? 如何在 javascript 上的另一个 js 文件中从 function 获取返回数据 - How to get return data from function in another js file on javascript 如何使用另一个 function 的返回生成一个新数组? Javascript - How to generate an new Array using a return from another function? Javascript
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM