首页» 前端 » webpack » Promise报错 Expected the Promise rejection reason to be an Error

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

日期:2018年12月16日 阅读次数:5282 分类:webpack

今天在使用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'))

即可

 
原文:https://blog.csdn.net/sinat_26229211/article/details/81164230

文章标签: