Skip to content

Incorrect use-before-declaration error in async IIFE initializerΒ #50123

Open
@Jessidhia

Description

@Jessidhia

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 promise variable.

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    BugA bug in TypeScriptHelp WantedYou can do this

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions