N
N
Nikita Bratsky2021-03-19 17:36:11
Node.js
Nikita Bratsky, 2021-03-19 17:36:11

How to get value from Async function?

const getter = async () => {
  let con;
  try
  {
    con = await pool.getConnection();
    rows = await con.query("SELECT * FROM table");
    return await rows;
  } 
  catch(err)
  {
    throw err;
  }
  finally
  {
    if(con)
    {
      return con.end();
    }
  }
};


After the call is:

socket.on('<имя события>', () =>{
    console.log("\n\n\nВывод Query:");
    getter().then((response_table) => console.log(response_table));
  	});


The problem is that my code outputs undefined on the line:
getter().then((response_table) => console.log(response_table))

spoiler
То что таблица без нормального имени и событие не описано нормально - специально убрал перед предоставлением кода
. Please tell me how to solve this problem.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
W
WbICHA, 2021-03-19
@NikBr_9

finally
  {
    if(con)
    {
      return con.end();
    }
  }

Read how finally works and think about what the function will return when there is a return in finally.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question