Skip to content

Declaring a function inside a try/catch prohibited by "no-inner-declaration" #1406

@davidystephenson

Description

@davidystephenson

I find myself wanting to create a function inside a try/catch block. For example:

async function uploadImages () {
  const profileUrl = buildProfileUrl()

  const picturesUrl = buildPicturesUrl()

  try {
    const profile = await getProfile(profileUrl)

    const pictures = await getPictures(picturesUrl)

    async function upload (picture) {
      return uploadImage({ profile, picture })
    }

    const promises = pictures.map(upload)
    
    return Promise.all(promises)
  } catch (error) {
    throw error
  }
}

I want to declare upload inside the try so I can take advantage of profile being in scope. However, this apparently violates the no-inner-declarations rule.

Can someone explain why this rule is important here? Should no-inner-declaration perhaps not apply to try/catch?

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    Status

    Done

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions