Ниже приведено содержимое файла, который я создал с помощью claude.ai, чтобы продемонстрировать проблему. Я не знаю, куда сообщить об ошибке, поэтому мой главный вопрос: действительно ли это ошибка, и если да, то документация (доступ к методу (.) имеет более низкий приоритет, чем ожидание) или реализация? Если это ошибка, куда ее следует сообщить? Если это не так, то что я неправильно истолковываю?
// Bug Report: Member Access (.) vs await Operator Precedence Inconsistency
// According to JavaScript's operator precedence table:
// - Member access (.) has precedence of 19
// - await has precedence of 16
// Therefore, .json() should be called before await is applied
// Setup: A function that returns a Promise that also has a json method
function getMockResponse() {
let p = Promise.resolve({
json: () => Promise.resolve({
data: 'test'
})
});
p.json = () => {
console.log('Promise.json called');
return Promise.resolve({
data: 'from Promise.json'
});
};
return p;
}
// The issue:
async function demonstratePrecedence() {
// These two expressions should be equivalent if . has higher precedence than await
console.log('Test 1: With direct chaining:');
const result1 = await getMockResponse().json();
console.log('result1:', result1); // Promise.json NOT called
console.log('\nTest 2: With parenthesized await:');
const result2 = (await getMockResponse()).json();
console.log('result2:', result2); // Different behavior!
}
// The following fixes work by explicitly controlling precedence:
async function demonstrateWorkarounds() {
console.log('\nTest 3: Forcing .json() first:');
const result3 = await (getMockResponse().json());
console.log('result3:', result3);
console.log('\nTest 4: Using two awaits:');
const result4 = await (await getMockResponse()).json();
console.log('result4:', result4);
}
/*
This demonstrates that despite member access (.) having higher precedence than await,
the await operator is being applied before the member access in practice.
If member access truly had higher precedence:
- getMockResponse().json() should be evaluated first
- This would call the Promise's json method (logging "Promise.json called")
- await would then be applied to the result
Instead:
- await getMockResponse() is evaluated first
- .json() is called on the resolved value
- The Promise's json method is never called
This inconsistency between documented precedence and actual behavior affects common patterns
in JavaScript, particularly when working with Promise-returning APIs that have chained methods.
Test this code by running:
async function test() {
await demonstratePrecedence();
await demonstrateWorkarounds();
}
test();
*/
demonstratePrecedence();
Ниже приведено содержимое файла, который я создал с помощью claude.ai, чтобы продемонстрировать проблему. Я не знаю, куда сообщить об ошибке, поэтому мой главный вопрос: действительно ли это ошибка, и если да, то документация (доступ к методу (.) имеет более низкий приоритет, чем ожидание) или реализация? Если это ошибка, куда ее следует сообщить? Если это не так, то что я неправильно истолковываю?
[code]// Bug Report: Member Access (.) vs await Operator Precedence Inconsistency
// According to JavaScript's operator precedence table: // - Member access (.) has precedence of 19 // - await has precedence of 16 // Therefore, .json() should be called before await is applied
// Setup: A function that returns a Promise that also has a json method
// The issue: async function demonstratePrecedence() { // These two expressions should be equivalent if . has higher precedence than await console.log('Test 1: With direct chaining:'); const result1 = await getMockResponse().json(); console.log('result1:', result1); // Promise.json NOT called
console.log('\nTest 2: With parenthesized await:'); const result2 = (await getMockResponse()).json(); console.log('result2:', result2); // Different behavior! }
// The following fixes work by explicitly controlling precedence: async function demonstrateWorkarounds() { console.log('\nTest 3: Forcing .json() first:'); const result3 = await (getMockResponse().json()); console.log('result3:', result3);
console.log('\nTest 4: Using two awaits:'); const result4 = await (await getMockResponse()).json(); console.log('result4:', result4); }
/* This demonstrates that despite member access (.) having higher precedence than await, the await operator is being applied before the member access in practice.
If member access truly had higher precedence: - getMockResponse().json() should be evaluated first - This would call the Promise's json method (logging "Promise.json called") - await would then be applied to the result
Instead: - await getMockResponse() is evaluated first - .json() is called on the resolved value - The Promise's json method is never called
This inconsistency between documented precedence and actual behavior affects common patterns in JavaScript, particularly when working with Promise-returning APIs that have chained methods.
Test this code by running: async function test() { await demonstratePrecedence(); await demonstrateWorkarounds(); } test(); */ demonstratePrecedence();[/code]