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

fix: check async generator #41

Merged

Conversation

TonyPythoneer
Copy link
Contributor

@TonyPythoneer TonyPythoneer commented Jul 22, 2024

Approaching the issues

Closes dapr/js-sdk#604

Root cause

I reviewed the code at this link and printed the result object if the Dapr Workflow function runs it. The output is as follows:

result: AsyncGenerator { _invoke: [Function (anonymous)] }
result.toString(): [object Object]

Based on the MDN documentation, this doesn't seem like the correct way to check if an object is an AsyncGenerator.

To properly check if result is an AsyncGenerator, we should use:

const isAsyncGenerator = typeof result[Symbol.asyncIterator] === 'function';

This approach is more reliable and aligns with the standard practices for identifying AsyncGenerator objects. Once this check is corrected, the workflow function should work as expected.

My thought

I opted not to write a test for this change because the new method, typeof result[Symbol.asyncIterator] === 'function', is more theoretically correct and robust than the previous string-based approach because:

  1. Direct Check: It directly checks for the presence of Symbol.asyncIterator, which is the defining characteristic of an async generator. This ensures we are accurately identifying async generators.
  2. Theoretical Correctness: The use of typeof to check for the existence of the async iterator method aligns with the theoretical underpinnings of JavaScript, making the check more reliable and less prone to errors due to changes in object string representations.

Given that the new implementation is a more theoretically correct and robust method for identifying async generators, I believe that writing a test for this specific change might not be necessary. The new method improves the accuracy and reliability of the check without introducing additional complexity or potential points of failure.

Additionally, the robustness of the new method implies that it inherently handles the scenarios we care about, reducing the need for extensive testing compared to the previous string-based approach.


Feel free to adjust any part of this if needed!

@TonyPythoneer
Copy link
Contributor Author

@kaibocai @cgillum @XavierGeerinck @microsoftopensource
Are you available to review my PR?

Thank you!

@XavierGeerinck XavierGeerinck merged commit f3e1fd9 into microsoft:main Jul 22, 2024
1 of 2 checks passed
@XavierGeerinck
Copy link
Collaborator

Thank you for the extensive research on this! Happy to merge it in

@TonyPythoneer TonyPythoneer deleted the fix/check-async-generator branch July 23, 2024 00:32
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Issue with SDK on workflow when using nestjs
2 participants