You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在生成器中处理未使用的yield返回值,避免no-unused-vars警告?

解决生成器中未使用变量警告的几种方法

针对你遇到的'res' is assigned a value but never used警告,不需要用console.log,可以用以下几种简洁的方式处理:

  • 直接不接收返回值:如果完全不需要第一个API调用的返回结果,直接去掉变量声明,只保留yield语句即可。这样既不会产生未使用变量的问题,也能确保API调用正常执行:

    function* cancelCredit(action) {
        yield call(/*Nice api call 1*/); // 不接收返回值
        const res2 =  yield call(/*Nice api call 2*/);
        // Do something with res2
        yield // something
    }
    
  • 用下划线_接收(社区约定忽略变量):如果想明确表示“我知道有返回值,但故意忽略”,可以用下划线作为变量名——这是JavaScript社区通用的忽略变量的约定,ESLint通常不会对_触发未使用变量警告:

    function* cancelCredit(action) {
        const _ = yield call(/*Nice api call 1*/); // 用_标记忽略返回值
        const res2 =  yield call(/*Nice api call 2*/);
        // Do something with res2
        yield // something
    }
    
  • 局部禁用ESLint规则(不推荐但可用):如果以上两种方式都不符合你的需求,可以在变量声明行添加ESLint注释临时禁用该规则:

    function* cancelCredit(action) {
        // eslint-disable-next-line no-unused-vars
        const res = yield call(/*Nice api call 1*/);
        const res2 =  yield call(/*Nice api call 2*/);
        // Do something with res2
        yield // something
    }
    

推荐优先用前两种方法,尤其是第一种,代码最简洁直观;第二种适合需要明确标注忽略返回值的场景。

内容的提问来源于stack exchange,提问作者Laurent Lyaudet

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.11 11:27:05