eslint-community / eslint-plugin-promise

Enforce best practices for JavaScript promises

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

5.1.0 - Each then() should return a value or throw

hawkeye64 opened this issue Β· comments

Updated deps this morning. In particular, eslint-plugin-promise from v4.3.1 to v5.1.0 (did you forget to make a tag for this release?) and getting the following error:

error  Each then() should return a value or throw  promise/always-return

The code in question looks like this:

export const fetchAbout = async function ({ commit, state, getters, rootState }) {
  return axios.get(state.baseUrl)
    .then((results) => {
      if (results.status === 200) { // OK
        commit('setAbout', results.data.payload)
        return results
      }
      else {
        throw new Error(results.data.error || results.data)
      }
    })
    .catch(function (error) {
      console.error('fetch about', error)
      throw error
    })
}

I am not seeing the issue here. There is both a return and a throw involved.
Expected behavior: No error here

Actual behavior: issues an error

Versions

  • Node version: v12.18.4
  • ESLint version: v7.23.0
  • eslint-plugin-promise version: v5.1.0

Anyone care to comment? Is the above code incorrect? If so, what would be the best procedure for handling this so the eslint-plugin-promise doesn't complain?

I believe this is a bug in plugin. I get this same error if return/throw contains an expression, just like you have inside your error constructor (results.data.error || results.data)

I can try to take a look at this this week.

I'm still seeing this issue in ver 6.0.0
The line reported in error has a logical expression like @Tol1 reported above in the construction of a literal object
and the character in the line is the d of data.title

    title: data.title || data.room,

which is an argument to a function call that returns a Promise at the beginning of a .then chain which is returned inside another .then function.

Versions

  • Node version: v16.13.2
  • ESLint version: v8.7.0
  • eslint-plugin-promise version: v6.0.0

eslint-plugin-promise v4.3.1 does not have this issue.

commented

This is a bug πŸ› (still present in v.6.0.0)

As @Tol1 , if you remove the expression inside Error() the error would go away!

promise-err

as a temp work around:

const errMsg = json?.error_description || 'Error';
throw new Error(errMsg);

πŸŽ‰ This issue has been resolved in version 6.1.0 πŸŽ‰

The release is available on:

Your semantic-release bot πŸ“¦πŸš€