Skip to main content

Comparison: Streaming Results

The Promise Helper Library handles streams without requiring callbacks or event listeners.

Without the Promise Helper Library, to stream results:

  • Execute the query with streamResult set to true.
  • Implement a complete callback that receives the statement object.
  • Use statement.streamRows() to create a stream.
  • Consume the results.
    • SDK documentation shows using callbacks on the stream's readable, error, and end events, but async iteration would be simpler.

Because of the way that callbacks work, code might not execute in the order you expect.

With the Promise Helper Library, to stream results:

  • Execute the query with streamResult set to true.
  • Use statement.streamRows() to create a stream.
  • Use async iteration to consume the results.

Examples

tip
  • Click "WITHOUT Helper Library" to see the original code
  • Click "WITH Helper Library" to see the improved code using this library

Based on an example from the Snowflake SDK documentation.

// Not using the Promise Helper Library

connection.execute({
sqlText:
"select L_COMMENT from SNOWFLAKE_SAMPLE_DATA.TPCH_SF100.LINEITEM limit 100000;",
streamResult: true,
complete: function (err, stmt) {
var stream = stmt.streamRows();
// Read data from the stream when it is available
stream
.on("readable", function (row) {
while ((row = this.read()) !== null) {
console.log(row);
}
})
.on("end", function () {
console.log("done");
})
.on("error", function (err) {
console.log(err);
});
},
});

console.log(
'This will likely execute before completion due to async behavior!'
);