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

?= violates the type safety of await #39

Open
alray2569 opened this issue Aug 28, 2024 · 3 comments
Open

?= violates the type safety of await #39

alray2569 opened this issue Aug 28, 2024 · 3 comments

Comments

@alray2569
Copy link

The await operator was designed to be type safe. That is, the await operator can operate on values of any type. When given a value that is not asynchronous, it returns its operand unchanged. For example, given the following:

async function get4Async() { return 4; }
function get4Promise() { return Promise.resolve(4); }
function get4Sync() { return 4; }

The following variables will all have the value of 4:

const n1 = await get4Async();
const n2 = await get4Promise();
const n3 = await (get4Promise());
const n4 = await get4Sync();
const n5 = await (get4Sync());
const n6 = await (Promise.resolve(4));
const n7 = await 4;

However, this does not work with the ?= polyfill:

const n1 ?= await get4Async();          // good
const n2 ?= await get4Promise();        // TypeError! get4Promise[Symbol.result] is not a function!
const n3 ?= await (get4Promise());      // good
const n4 ?= await get4Sync();           // TypeError! get4Sync[Symbol.result] is not a function!
const n5 ?= await (get4Sync());         // TypeError! (get4Sync())[Symbol.result] is not a function!
const n6 ?= await (Promise.resolve(4)); // good
const n7 ?= await 4;                    // TypeError! 4[Symbol.result] is not a function!

Even though all of these work with regular assignment, four of them throw a TypeError when used with "safe" assignment. Note of particular concern that the extra parentheses in n3, which are redundant with normal assignment, are now required with safe assignment!

@arthurfiorette
Copy link
Owner

We will not use the ?= syntax anymore.

#4

I just need more spare time to be able to rewrite it :)

@arthurfiorette
Copy link
Owner

Also, why this example would throw TypeError?

const n2 ?= await get4Promise();        // TypeError! get4Promise[Symbol.result] is not a function!

get4Promise is a function and thus would have Symbol.result in its prototype.

@alray2569
Copy link
Author

@arthurfiorette Because the proposal said specifically that async functions would define Symbol.result. It did not say that all functions would define Symbol.result. Since get4Promise was not defined as an async function, it would therefore not have Symbol.result.

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

No branches or pull requests

2 participants