node.jscassandrasynchronouscassandra-driverdatastax-node-driver

Problem with Synchronous query execution in cassandra-driver (Node.js)


I'm trying to create at runtime keyspaces and tables inside them using cassandra-driver node implementation maintained by datastax.

On the documentation (here) it's pointed out that there is a sync-way to call the execute() method that should block the execution until the the result from the query is returned.

I think that examples (both for sync and async section for Node.js) linked at this url are only for async execution, because they didn't block until result is returned.

I'm missing something or is not possible to achieve a synchronous execution of a query with Node.js driver?


Solution

  • Node.js libraries that use I/O are generally async only. That's the case for all/most DB drivers.

    Given how Node.js event loop works, sync execution is usually not what you want.

    If you want a syntax that is as comfortable to synchronous execution in Node.js, you should use async functions:

    async function doSomething(client) {
      await client.execute(query1);
      // ...
      await client.execute(query2);
      await client.execute(query3);
    }