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

如何解决ESLint报Expected to return a value at the end of arrow function错误

报错原因

该错误由ESLint的consistent-return规则触发,规则要求函数所有执行分支要么都有返回值,要么都没有返回值。你的代码仅在isRemoteAvailable为真、且try块无异常抛出时才返回值,另外两种场景没有返回值:

  • isRemoteAvailable为假时,函数直接执行结束无返回
  • try块抛出异常进入catch分支后,仅打印错误无返回
修复方案

建议统一在函数末尾返回结构一致的对象,所有分支都有明确返回值,同时也方便调用方处理逻辑:

export const setupRemote = () => {
  let allowAppInstance = null;
  if (isRemoteAvailable) {
    try {
      // ... 保留原有执行逻辑
      allowAppInstance = SetupConfig.start(remoteInstance);
      window.setup = {
        someconfig: anothercode,
        code: somecode,
      };
    } catch (e) {
      console.error(e);
    }
  }
  // 统一在末尾返回,保证所有分支都有返回值
  return { allowAppInstance };
};

如果需要更精细的错误提示,也可以在返回对象中增加错误字段,方便调用方定位问题:

export const setupRemote = () => {
  let allowAppInstance = null;
  let error = null;
  if (isRemoteAvailable) {
    try {
      // ... 保留原有执行逻辑
      allowAppInstance = SetupConfig.start(remoteInstance);
      window.setup = {
        someconfig: anothercode,
        code: somecode,
      };
    } catch (e) {
      error = e;
      console.error(e);
    }
  } else {
    error = new Error('remote service is not available');
  }
  return { allowAppInstance, error };
};
调用示例

其他组件调用时直接判断返回的实例是否存在即可:

import { setupRemote } from './你的文件路径';

const { allowAppInstance, error } = setupRemote();
if (allowAppInstance) {
  // 初始化成功,正常使用实例
} else {
  // 初始化失败,走降级逻辑或提示错误
  console.error('初始化失败', error);
}

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.10.19 09:54:04