Skip to content

for await of loops don't call AsyncIterator.return() on early exitΒ #52936

@martinbirks

Description

@martinbirks

Bug Report

πŸ”Ž Search Terms

Async return AsyncIterator

πŸ•— Version & Regression Information

  • This changed between versions 4.8.4 and 4.9.5

⏯ Playground Link

Playground link with relevant code

πŸ’» Code

const LIMIT = 3;

const asyncIterable = {
  [Symbol.asyncIterator]() {
    let i = 0;
    return {
      next() {
        const done = i === LIMIT;
        const value = done ? undefined : i++;
        return Promise.resolve({ value, done });
      },
      return() {
        console.log("RETURN");
        // This will be reached if the consumer called 'break' or 'return' early in the loop.
        return { done: true, value: undefined };
      },
    };
  },
};

(async () => {
  for await (const num of asyncIterable) {
    console.log(num);
    break;
  }
})();

πŸ™ Actual behavior

Outputted "0", the return function of the async iterator was never called.

πŸ™‚ Expected behavior

Output "0" and "RETURN" to the log. This would show that the return() function was called.

Metadata

Metadata

Assignees

Labels

Fix AvailableA PR has been opened for this issueNeeds InvestigationThis issue needs a team member to investigate its status.

Type

No type

Projects

No projects

Relationships

None yet

Development

No branches or pull requests

Issue actions