-1

How do I store something from a conn.execute complete block?

https://docs.snowflake.com/en/user-guide/nodejs-driver-use.html#executing-statements

Basically I want to do something like this:

async function checkRecords(conn: Connection, sqlText: string): Promise<number> {
  return new Promise ((resolve, reject) => {
    try {
      conn.execute({
        sqlText: sqlText,
        complete: function(err, stmt, rows) {
          if (err) {
            reject(err);
          } else {
            let ret = parseInt(rows[0].COUNT);
            return Promise.resolve(ret);
          }
        }
      });
    } catch (err)  {
      error(err);
    }
  });
}

I think my question is similar to How can I execute Snowflake statement in async mode using NodeJs Snowflake driver? but I did not find any answer there.

Because of the async nature of the complete I never manage to return the value of the complete block.

I've tried to make it await, I've tried to make the function async and return a promise the point is that when I then call the function it still always ignores the wait (and I see in the log the executions with the actual result after my code that was supposed to wait for it already moved one).

Honestly I did not find any good example of this based in Snowflake SDK so I was wondering if anyone knows of a good example to test thigs.

I've seen a lot of different posts on javascript about this like How to return the response from an asynchronous call

But honestly I do not get it so I really wanted to know if someone has some example based on the Snowflake SDK I could use for inspiration.

I will close this question but somehow I can't make my code wait on the promise

Basically this code just does nothing.

  checkRecords(conn, stmtTextStage).then(value => {
    if (value > 0) {
      log(`STAGE.${name} contains ${value} unique records!`);
    } else {
      log(`STAGE.${name} contains no records!`, 'WARN');
    }
  });
Miguel Costa
  • 627
  • 1
  • 12
  • 30
  • 1
    Does this answer your question? [How to return the response from an asynchronous call](https://stackoverflow.com/questions/14220321/how-to-return-the-response-from-an-asynchronous-call) – E_net4 Jan 27 '22 at 15:32
  • not really because I'm really interested in the snowflake side and not so much the javascript side. I mean thanks you for the feedback but maybe I'm missing something but my question is not so much the javascript side but the implementation of the snowflake execute method and how can I make my functions wait for it to process – Miguel Costa Jan 27 '22 at 15:41
  • There is no difference. Even using snowflake, those are typical JavaScript asynchronous calls, which must also be used in an asynchronous environment. Either adjust `checkRecords` to return a promise or add a callback parameter, as described in the linked question. – E_net4 Jan 27 '22 at 15:51
  • @E_net4thecurator thanks for suggesting edits but you are basically removing all my question and details I'm trying to provide. Also I mainly want to focus this in whoever has experience with the Snowflake SDK for javascript not the full javascript community – Miguel Costa Jan 27 '22 at 15:51
  • @E_net4thecurator thanks for your hint about making the function synchronous I was not getting it but eventually I think I got there – Miguel Costa Jan 27 '22 at 16:53

1 Answers1

0

well I did manage to make it work but took me a while because I really don't understand this Promise resolve thing but something like this did work:

async function checkRecords(conn: Connection, sqlText: string): Promise<number> {
  return new Promise ((resolve, reject) => {
    try {
      conn.execute({
        sqlText: sqlText,
        complete: function(err:SnowflakeError, stmt: Statement, rows: any[]) {
          if (err) {
            error(`${stmt.getSqlText()} : ${err.code}`);
            reject(0);
          } else {
            if (rows.length === 1) {
              resolve(parseInt(rows[0].COUNT));
            } else {
              error(`${sqlText} returned un-expeted rows!`);
              reject(0);
            }
          }
        }
      });
    } catch (err) {
      error(err);
    }
  });
}
Miguel Costa
  • 627
  • 1
  • 12
  • 30