title: How can you await the result of a js callback (using the Snowflake SQL API)?
body: <p>I'm not sure if I'm overcomplicating this, but I don't do js very often... I'm trying to use the Snowflake SQL Node.js library, which includes this example for consuming SQL query results:</p>
  <p><a href="https://docs.snowflake.com/en/user-guide/nodejs-driver-use.html#consuming-results" rel="nofollow noreferrer">https://docs.snowflake.com/en/user-guide/nodejs-driver-use.html#consuming-results</a></p>
  <pre><code>connection.execute({
    sqlText: 'select * from sometable',
    complete: function(err, stmt, rows) {
      if (err) {
        console.error('Failed to execute statement due to the following error: ' + err.message);
      } else {
        console.log('Number of rows produced: ' + rows.length);
      }
    }
  });
  </code></pre>
  <p>I also need to use SSO to auth to the DB, for which they provide this example, which will spawn a browser window when it's run:</p>
  <p><a href="https://docs.snowflake.com/en/user-guide/nodejs-driver-use.html#using-single-sign-on-sso-through-a-web-browser" rel="nofollow noreferrer">https://docs.snowflake.com/en/user-guide/nodejs-driver-use.html#using-single-sign-on-sso-through-a-web-browser</a></p>
  <pre><code>// Use a browser to authenticate via SSO.
  var connection = snowflake.createConnection({
    ...,
    authenticator: &quot;EXTERNALBROWSER&quot;
  });
  // Establish a connection. Use connectAsync, rather than connect.
  connection.connectAsync(
    function (err, conn)
    {
      ... // Handle any errors.
    }
  ).then(() =&gt;
  {
    // Execute SQL statements.
    var statement = connection.execute({...});
  });
  </code></pre>
  <p>Here's the problem: I want to return the result of a <code>SELECT</code> statement back to some other code I'm running. I can do something like this:</p>
  <pre><code>async function querySnowflake(input) {
    const query = `...`
    var connection = snowflake.createConnection({...});
    await connection.connectAsync(
      function (err, conn) {
        if (err) {
          console.error('Connection to Snowflake failed');
        } else {
          var statement = connection.execute({
            sqlText: query,
            complete: function (err, stmt, rows) {
              if (err) {
                console.error('Failed to execute statement due to the following error: ' + err.message);
              } else {
                return rows;
              }
            }
          });
        }
      }
    );
  </code></pre>
  <p>But I have no idea how to return the rows selected by the query back to the part of my code that calls <code>querySnowflake()</code> in the first place, because if I await the connection itself, the function gets returned immediately after I perform the auth function, and the callback never fires. I think that both the documentation and my creativity in using js async are lacking, and I'm not sure how to proceed here...</p>