CodeG
CodeG

Reputation: 467

How can I fix this error coming from eslint?

Is there a better way to write this part of the code? It works, but the eslint is complaining that

'resp' is already declared in the upper scope

In this part

    return parseJSON(resp).then((resp) => {
      throw resp
    })

This is the entire code

  componentDidMount = async () => {
    const parseJSON = (resp) => (resp.json ? resp.json() : resp)

    const checkStatus = (resp) => {
      if (resp.status >= 200 && resp.status < 300) {
        return resp
      }
      return parseJSON(resp).then((resp) => {
        throw resp
      })
    }
    const headers = {
      'Content-Type': 'application/json'
    }

    try {
      const data = await fetch('http://myurl/api', {
        method: 'GET',
        headers: headers
      }).then(checkStatus)
        .then(parseJSON)
      this.setState({ data })
    } catch (error) {
      this.setState({ error })
    }
  }

Upvotes: 0

Views: 69

Answers (1)

Muhammad Aftab
Muhammad Aftab

Reputation: 1118

You have variable name resp in twice in the same scope on below lines:

const checkStatus = (resp) => {

and return parseJSON(resp).then((resp) => {.

change one of resp.

Upvotes: 1

Related Questions