首页 > 解决方案 > 如何在 .then() JS 中返回

问题描述

我有以下代码:

const fetchLoginData = async (): Promise<boolean> => {

  fetch(urlWithParams, options)
    .then((response) => response.json())
    .then((json) => json.data.account.authentication_token)
    .then((auth_token) => {
      console.log(auth_token);

      SecureStore.setItemAsync('Auth_Token', auth_token);
      // here I want ot return TRUE
    })
    .catch((error) => {
      console.error(error);
      // here I want ot return FALSE
    });

但是,如果我只是添加return true/false而不是评论 - TS linter 会警告我,如果我根本没有回报。那么内部有条件返回的正确方法是什么fetch()

标签: javascripttypescript

解决方案


推荐阅读