微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

如何让 getter 返回一个承诺

如何解决如何让 getter 返回一个承诺

我希望我的班级返回我从 URL 中获取用户

private IEnumerable<T> GetRealizadosAcumulados<T>(string proyectoId,IDbConnection dataBase,string sql)
  where T:IAcumulados
{
  var queryResults = dataBase.Query<dynamic>(sql,new { proyectoId = proyectoId },commandType: CommandType.StoredProcedure);

  return queryResults.Select(item => new T()
  {
    ClienteId = item.ClienteId,NombreCliente = item.NombreCliente,ProyectoId = item.ProyectoId,Realizado = item.RealizadoAcumulado,Planificado = item.PlanificadoAcumulado
  });

}

在另一个函数中,我尝试像这样调用这个类:

export default class CurrentUser{
    get user(){
        return this.fetch_user();
    }

    fetch_user(){
        fetch("http://127.0.0.1:8000/api/users/current",{
            headers: {
            'Content-Type': 'application/json',Authorization: `Token ${localStorage.getItem('token')}`
            }
        })
        .then(res => res.json())
        .then((user) => {
            return user;
        })
    }
}

直到现在返回的只是一个空对象。 我认为当我从我的 getter 中调用我的函数 fetch_user() 时,该函数在 fetch 请求和 promise 完成之前返回。

我尝试在下面实施这些解决方案,但没有成功。

How to block for a javascript promise and return the resolved result?

How to return promise with a return value

How would one do async JavaScript getters and setters?

请帮帮我。

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。