Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

160
Visualizações
Asynchronous Axios Initiation

I am trying to initiate an Axios Client for my React app. I want to use the authenticated client to make requests. The current setup looks as followed. Due to the session service being async to check for a cached token and validating it against the server the initiation of the API Client is also async. This results in the client in the ExampleService being undefined when constructed too early. The goal is to keep the Services as self contained as possible.

class ApiClientService {

    client: AxiosInstance

    async init() { // gets called asap on launch
        await sessionService.init()

        this.client = axios.create({
            baseURL: config.host,
            timeout: 1000,
            headers: { Authorization: `token ${sessionService.getToken()}` }
        })
    }

    getClient() {
        return this.client
    }

    ...
}



class ExampleService {

    private client = apiService.getClient()

    async doSomething() {
      return this.client.get('/something')

   }

}
about 4 years ago · Juan Pablo Isaza
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda