Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Promise returned from sync dispose method should not be awaited when disposing an asyc-disposable #58077

Closed
JLHwung opened this issue Apr 4, 2024 · 1 comment Β· Fixed by #58624
Assignees
Labels
Bug A bug in TypeScript Fix Available A PR has been opened for this issue

Comments

@JLHwung
Copy link

JLHwung commented Apr 4, 2024

πŸ”Ž Search Terms

using

πŸ•— Version & Regression Information

I assume it is in every version that using has been implemented

⏯ Playground Link

https://www.typescriptlang.org/play?ts=5.5.0-dev.20240404#code/BQQwzgngdgxgBMAlHAvAPjgbwFAwPZRgAucANngOapwDaAugNzbZxz6EkAOATngLYBLMAFMAIkM54R1KMIDucAAq9BI4MG7CweUgDdhAGjgB9ZOiwtWcEUQAqAvsLwBXIurMYcVq+QoA6TmcwAAtgACIIOAATCSlhOB5+IXiAIzwoiDDEJm9WTW09YSQcqwBfIwAGbMtS6ssvKxA5EAESIIEoKgAPagbvcGh4GgBlCD400j8B2HEwSWkUOFHxnT8AMzxucOWJqcgZ2JEsuiQLXJ9KAKDQsJ7pmFn51PTM6vPyy1ZSktYmlrawB0qJFFn0rCMxrsYnM4tQdqsNlswvDJtCnsdTmDvL4riFwpE0bC0hksj9cpoiM5uFAEipko84mSvgZPnBvjVmKwcYE8WFia8mJY-q1aUkRAyRILWOwCsI-L5gL5qrVikA

πŸ’» Code

(async () => {
  const log = [];

  const promiseDispose = new Promise((resolve, _) => {
    setTimeout(() => {
      log.push("y dispose promise body");
      resolve();
    }, 0);
  });

  {
    await using x = {
      async [Symbol.asyncDispose = Symbol.for("Symbol.asyncDispose")]() {
        log.push("x asyncDispose body");
      },
    };
    await using y = {
      [Symbol.dispose = Symbol.for("Symbol.dispose")]() {
        log.push("y dispose body");
        return promiseDispose;
      },
    };
  }

  log.push("body");

  await promiseDispose;

  console.log(log);
})();

πŸ™ Actual behavior

It logs ["y dispose body", "y dispose promise body", "x asyncDispose body", "body"]

πŸ™‚ Expected behavior

It should log ["y dispose body", "x asyncDispose body", "body", "y dispose promise body"].

The "y dispose promise body" should be printed in the next event cycle, however it is printed before x asyncDispose body because the promise is awaited when disposing y.

Additional information about the issue

Per 7.5.6 GetDisposeMethod

NOTE: This function is not observable to user code. It is used to ensure that a Promise returned from a synchronous dispose method will not be awaited.

cc @rbuckton

@rbuckton
Copy link
Member

rbuckton commented Apr 4, 2024

Thanks, I plan to fix this shortly along with several other small normative changes up for discussion at next week's TC39 plenary.

@RyanCavanaugh RyanCavanaugh added the Bug A bug in TypeScript label Apr 4, 2024
@RyanCavanaugh RyanCavanaugh added this to the TypeScript 5.6.0 milestone Apr 4, 2024
@typescript-bot typescript-bot added Fix Available A PR has been opened for this issue labels May 22, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug A bug in TypeScript Fix Available A PR has been opened for this issue
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants