Skip to content

Suppost for using and async using syntax #472

Closed
@langpavel

Description

@langpavel

Is your feature request related to a problem? Please describe.

Deno already supports using and await using syntax with some APIs

Describe the solution you'd like

Instead of this:

{
  const client = await pgPool.connect();
  try {
    // ... await some work
  } finally {
    client.release()
  }
}

I wish write this:

{
  using client = await pgPool.connect();
  // ... await some work
} // client is released automatically

In addition, second step

Similar syntax can be used for transactions too, for example:

{
  using client = await pgPool.connect();
  await using transaction = await client.begin();
  await transaction.queryArray(...);
  await transaction.commit(); // otherwise it will rollback
}
// - automatically rollback transaction if not commited and
//   report, that transaction was not properly closed (either commited or rolled back)
// - release client

Advanced example:

{
  using client = await pgPool.connect();
  try {
    await using transaction = await client.begin();
    await transaction.queryArray(...);
    if (somethingBadHappened) {
      await transaction.rollback();
    }
    await transaction.commit();
  }
  // automatically rollback transaction if not commited and
  // report, that transaction was not properly closed (either commited or rolled back)
  catch(error)
  {
    // transaction is rolled back
    console.log(error);
  }
} // release client

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions