Dan Aprahamian (2018-10-15T18:40:28.000Z)
Hello all! First time posting here. I was curious if there was ever talk
about adding something similar to python's with syntax to JS. Obviously we
already have a "with" keyword, but I figure we could probably use "use" as
the keyword instead. I was thinking something like

// Some user defined resource that can be "entered" and "exited"
class MyResource {
  // Function called when entered
  async [Symbol.enter]() {
  }

  // Function called when exited
  [Symbol.exit]() {
  }
}

const resource = new MyResource();

async function useResource() {
  use myResource {
    // Inside here, resource has had "acquire" called on it

    await doSomethingAsync();
    // Once this block is finished executing (including async)
    // release is called
  }
  // Release has been called now
}

Use would effectively be the equivalent of:

async function use(resource, body) {
  await resource[Symbol.enter]();
  try {
    await body();
  } finally {
    await resource[Symbol.exit]();
  }
}

Has something like this been considered before?

Thanks,
Dan
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.mozilla.org/pipermail/es-discuss/attachments/20181015/4e68589a/attachment.html>
dan.aprahamian at gmail.com (2018-10-15T18:45:49.643Z)
Hello all! First time posting here. I was curious if there was ever talk
about adding something similar to python's with syntax to JS. Obviously we
already have a "with" keyword, but I figure we could probably use "use" as
the keyword instead. I was thinking something like

    // Some user defined resource that can be "entered" and "exited"
    class MyResource {
      // Function called when entered
      async [Symbol.enter]() {
      }

      // Function called when exited
      [Symbol.exit]() {
      }
    }

    const resource = new MyResource();

    async function useResource() {
      use myResource {
        // Inside here, resource has had "enter" called on it

        await doSomethingAsync();
        // Once this block is finished executing (including async)
        // exit is called
      }
      // exit has been called now
    }

Use would effectively be the equivalent of:

    async function use(resource, body) {
      await resource[Symbol.enter]();
      try {
        await body();
      } finally {
        await resource[Symbol.exit]();
      }
    }

Has something like this been considered before?

Thanks,
Dan