Promise报错 Expected the Promise rejection reason to be an Error

今天在使用promise的时候

getLyric() {
    if (this.lyric) {
      return Promise.resolve(this.lyric)
    }
    return new Promise((resolve, reject) => {
      getLyric(this.mid).then((res) => {
        if (res.retcode === ERR_OK) {
          this.lyric = Base64.decode(res.lyric)
          resolve(this.lyric)
        } else {
          reject('no lyric')
        }
      })
    })

这样写总是会在控制台报出:
http://eslint.org/docs/rules/prefer-promise-reject-errors Expected the Promise rejection reason to be an Error
src\common\js\song.js:30:11
reject(‘no lyric’)
这样的做错,但是并不影响运行.
作为一个容不下错误的程序员,仔细研究后发现.
在promise的reject中需要传入的是一个Error对象.
因此将
reject(‘no lyric’)
改为
reject(new Error(‘no lyric’))
即可

你可能感兴趣的:(前端)