首页 > 解决方案 > Return const -- 期望在箭头函数结束时返回一个值

问题描述

我有一个设置服务的组件,我需要在我的应用程序的其他组件中接收该服务。

这是设置代码:

export const setupRemote = () => {
  if (isRemoteAvailable) {
    try {
      ...

      const allowAppInstance = SetupConfig.start(remoteInstance);

      window.setup = {
        someconfig: anothercode,
        code: somecode,
      };
    } catch (e) {
      console.error(e);
    }
  }
};

我需要返回const allowAppInstance,我试图这样做

export const setupRemote = () => {
  if (isRemoteAvailable) {
    try {
      ...

      const allowAppInstance = SetupConfig.start(remoteInstance);

      window.setup = {
        someconfig: anothercode,
        code: somecode,
      };

      return { 
         allowAppInstance
      }
    } catch (e) {
      console.error(e);
    }
  }
};

但是我不能使用return,在es-lint中会出现这个错误——预计在箭头函数的末尾返回一个值

如何返回此 const 以在我的应用程序的其他部分使用?

标签: javascriptreactjseslint

解决方案


这里的问题似乎是您没有在 eslint 期望的 catch 块中返回任何内容。请在 catch 块中添加一些 return 并检查函数是否是您的远程实例并采取相应措施。


推荐阅读