0

Here is the code I have tried, when I try to return soc it says it is undefined, I know this is because it is defined within a nested promise. My question is how can I return the result of my sql query without the use of a .then promise as I do not fully understand them and they're just causing me pain.

async function get_charge_level() {
    var config = {
            <config_settings>
        };
        sql.setDefaultConfig(config);
        await sql.execute( {
            query: "some query"
        } ).then(async function( id ) {

            await sql.execute( {
                query: "some other query"
            } ).then(async function( soc ) {
            })
        })

    return soc
}
EcSync
  • 842
  • 1
  • 6
  • 20
  • Have look [here](https://developer.mozilla.org/de/docs/Web/JavaScript/Reference/Operators/await). You're mixing the `await` operator with the promises. `await` waits for the promise to be resolved and returns the result. – Kryptur Jul 25 '19 at 12:04
  • Avoid [`await`ing a `.then(…)` chain](https://stackoverflow.com/a/54387912/1048572)! If you don't want to use `then`, just use `await` instead. – Bergi Jul 25 '19 at 12:32

1 Answers1

1

You're already using the async-await syntax, so just change as follows:

async function get_charge_level() {
  var config = {
    //<config_settings>
  }
  sql.setDefaultConfig(config);
  let id = await sql.execute({
    query: "some query"
  })
  let soc = await sql.execute({
    query: "some other query"
  })
  return soc
}
Anurag Srivastava
  • 14,077
  • 4
  • 33
  • 43