【问题标题】:Dealing with Promise executor functions ESLint error处理 Promise 执行器函数 ESLint 错误
【发布时间】:2021-10-09 21:25:51
【问题描述】:

以下代码块引发两个警告

  1. ESLint:Promise 执行器函数不应该是异步的。 (no-async-promise-executor)
  2. ESLint:在预期返回 void 的函数参数中返回 Promise。 (@typescript-eslint/no-misused-promises)

重写它以消除错误消息的最佳方法是什么?

async signIn(email: string, password: string, redirectTo: string): Promise<unknown> {
  return new Promise(async (resolve, reject) => {
    const { error, data } = await this.supabaseClient.auth.signIn(
      { email: email, password: password },
      { redirectTo: redirectTo }
    );
    if (error) {
      reject(error);
    } else {
      resolve(data);
    }
  });
}

【问题讨论】:

    标签: typescript eslint


    【解决方案1】:
    async signIn(email: string, password: string, redirectTo: string) {
        const { error, data } = await this.supabaseClient.auth.signIn(
          { email: email, password: password },
          { redirectTo: redirectTo }
        );
        if (error) {
          throw new Error(error);
        }
    
        return data
      });
    }
    

    完全不需要使用promise,可以抛出错误。

    【讨论】:

      猜你喜欢
      • 2015-09-06
      • 2015-08-28
      • 2016-09-29
      • 2015-08-07
      • 2018-09-15
      • 2019-02-17
      • 1970-01-01
      • 2016-12-08
      • 2014-03-15
      相关资源
      最近更新 更多