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

feat: Safe assignment polyfill V2, introducing Function#result() method #47

Closed

Conversation

otaxhu
Copy link

@otaxhu otaxhu commented Sep 18, 2024

This pull request solves some problems discussed on many issues:

credits to @rockedpanda comment in #45 issue.

  • It doesn't introduce special syntax nor punctuation tokens that is very hard to parse or understand by humans.
    This problem was mentioned in this issues: Unexpected parsing inconsistency #38
  • It doesn't bring up order-matters problems.
    This problem was mentioned in this issues: Prior art to safe assignment operator proposal #41
    It basically talks about the order of the following expressions:
    // most likely this is the only correct way, since we wrapping await
    // with try expression;
    const [parseError, json] = try await response.json();
    
    // anyways most likely there will be such cases, and this will be syntax error
    // on Babel, ESLint and Typescript
    // or runtime error when json has promise, instead of result
    const [parseError, json] = await try response.json();

Some examples usage of this new polyfill:

// Function.result() returns a promise if the underlying function returns a promise.
const [error, response] = await fetch.result("https://example.org");
// You can't forget the order, any function has the method result (even async functions)

const [error, obj] = JSON.parse.result("bad");

@otaxhu otaxhu closed this Sep 18, 2024
@otaxhu otaxhu deleted the result-method-no-symbol-polyfill branch September 18, 2024 21:43
@otaxhu otaxhu restored the result-method-no-symbol-polyfill branch September 18, 2024 21:44
@otaxhu otaxhu deleted the result-method-no-symbol-polyfill branch September 18, 2024 21:44
@otaxhu
Copy link
Author

otaxhu commented Sep 18, 2024

I didn't test it before opening the pull request, it has a lot of problems with Window object functions like fetch(), the this context changes when calling fetch.result() method, making the call to throw with an error TypeError: 'fetch' called on an object that does not implement interface Window.

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.

1 participant