Comparison: Connecting to Snowflake
This example shows how connecting to Snowflake works.
Without the Promise Helper Library, you provide a callback that handles the connection result. Code outside of the callback can't tell when the connection is established, so you should call connection.isValidAsync()
to check if the connection is ready for queries.
With the Promise Helper Library, you simply await the connection.
Examples
tip
- Click “WITHOUT Helper Library” to see the original code
- Click “WITH Helper Library” to see the improved code using this library
- WITHOUT Helper Library
- WITH Helper Library
Example from Snowflake SDK documentation.
// Not using the Promise Helper Library
// Create a Connection object that we can use later to connect.
var connection = snowflake.createConnection({
account: account,
username: user,
password: password,
application: application,
});
// Try to connect to Snowflake, and check whether the connection was successful.
connection.connect(function (err, conn) {
if (err) {
console.error("Unable to connect: " + err.message);
} else {
console.log("Successfully connected to Snowflake.");
// Optional: store the connection ID.
connection_ID = conn.getId();
}
});
// Verify if connection is still valid for sending queries over it.
const isConnectionValid = await connection.isValidAsync();
// Do further actions based on the value (true or false) of isConnectionValid
// Using the Promise Helper Library
// Create a Connection object and promisify it
const connection = promisifyConnection(
snowflake.createConnection({
account: account,
username: user,
password: password,
application: application,
}),
);
// Connect to Snowflake
try {
await connection.connect();
console.log("Successfully connected to Snowflake.");
} catch (err) {
console.error("Unable to connect: " + err.message);
}
// Optional: store the connection ID.
const connection_ID = connection.getId();
// Now you can use the Connection object to execute queries