-
Notifications
You must be signed in to change notification settings - Fork 13.1k
Open
Labels
BugA bug in TypeScriptA bug in TypeScriptDomain: BinderGenerally crashes in, or caused by, the binder not doing something rightGenerally crashes in, or caused by, the binder not doing something rightHelp WantedYou can do thisYou can do this
Milestone
Description
Bug Report
π Search Terms
- dead zone
- await tdz
- use before declaration await
π Version & Regression Information
- This is the behavior in every version I tried, and I reviewed the FAQ for entries about await
β― Playground Link
Playground link with relevant code
π» Code
const promise = (async () => {
await null
promise
})()Real life code sample affected by the error
useEffect(() => {
const request = (async () => {
try {
const response = await premiumApi.announcements(client)
// @ts-expect-error TypeScript bug, assumes "request" is still in dead zone after await
if (pendingRequest.current === request || pendingRequest.current === undefined) {
pendingRequest.current = undefined
setApiResponse(response)
}
} catch {
pendingRequest.current = undefined
}
})()
pendingRequest.current = request
return () => {
pendingRequest.current = undefined
}
}, [client])π Actual behavior
- "Block-scoped variable 'promise' used before its declaration."
- "Variable 'promise' is used before being assigned."
π Expected behavior
- No errors related to the
promisevariable.
This is not about the "'await' has no effect on the type of this expression" warning, this is a feature and is expected despite me disagreeing with it.
The first await to be reached in an async function is the point where the function stops being synchronous and yields a Promise for its result. This means that, immediately after the await, the const promise is no longer in TDZ and is well-defined: it is the not-yet-resolved Promise that was produced by the IIFE's invocation. Now, awaiting the promise would be a deadlock, as the promise would now be waiting for itself to resolve before being able to resolve, but the Promise instance itself is still useful for some purposes like === checks.
whzx5byb
Metadata
Metadata
Assignees
Labels
BugA bug in TypeScriptA bug in TypeScriptDomain: BinderGenerally crashes in, or caused by, the binder not doing something rightGenerally crashes in, or caused by, the binder not doing something rightHelp WantedYou can do thisYou can do this