0

I want to assign the variable salary with the result of the promise. How can I do that?

  host: hostName,
  user: user,
  password: password,
  port: portNumber,
});

var salary;

function SelectSum (con) {
  return new Promise ((resolve, reject)=>{
    con.query("SELECT sum(salary) as sum FROM people'", (error, sum)=>{
      if (error) {
        return reject (error);
      }
      return resolve(sum);
    })
  })
}```
  • Could you share a bit more code and add some more explanation to the question? Here you can find [How to ask a good question](https://stackoverflow.com/help/how-to-ask) – Damian Aug 22 '22 at 18:48
  • You can't really de-async async code. You'll want to `await`/`then` the promise anywhere you need its value, so it's not possible to say `var salary = SelectSum(con)` synchronously (`salary` would just be an unfulfilled promise here). Try something like `SelectSum(con).then(sum => /* use sum */)`. Please read [the canonical thread](https://stackoverflow.com/questions/14220321/how-do-i-return-the-response-from-an-asynchronous-call) thoroughly. – ggorlen Aug 22 '22 at 18:49
  • Let me know if the dupe doesn't solve it for you, after you've read it, and I can reopen for you if you can explain your problem and goal a bit more specifically. – ggorlen Aug 22 '22 at 18:59

0 Answers0